1/* ELF linking support for BFD. 2 Copyright (C) 1995-2017 Free Software Foundation, Inc. 3 4 This file is part of BFD, the Binary File Descriptor library. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 19 MA 02110-1301, USA. */ 20 21#include "sysdep.h" 22#include "bfd.h" 23#include "bfd_stdint.h" 24#include "bfdlink.h" 25#include "libbfd.h" 26#define ARCH_SIZE 0 27#include "elf-bfd.h" 28#include "safe-ctype.h" 29#include "libiberty.h" 30#include "objalloc.h" 31#if BFD_SUPPORTS_PLUGINS 32#include "plugin-api.h" 33#include "plugin.h" 34#endif 35 36/* This struct is used to pass information to routines called via 37 elf_link_hash_traverse which must return failure. */ 38 39struct elf_info_failed 40{ 41 struct bfd_link_info *info; 42 bfd_boolean failed; 43}; 44 45/* This structure is used to pass information to 46 _bfd_elf_link_find_version_dependencies. */ 47 48struct elf_find_verdep_info 49{ 50 /* General link information. */ 51 struct bfd_link_info *info; 52 /* The number of dependencies. */ 53 unsigned int vers; 54 /* Whether we had a failure. */ 55 bfd_boolean failed; 56}; 57 58static bfd_boolean _bfd_elf_fix_symbol_flags 59 (struct elf_link_hash_entry *, struct elf_info_failed *); 60 61asection * 62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie, 63 unsigned long r_symndx, 64 bfd_boolean discard) 65{ 66 if (r_symndx >= cookie->locsymcount 67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) 68 { 69 struct elf_link_hash_entry *h; 70 71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; 72 73 while (h->root.type == bfd_link_hash_indirect 74 || h->root.type == bfd_link_hash_warning) 75 h = (struct elf_link_hash_entry *) h->root.u.i.link; 76 77 if ((h->root.type == bfd_link_hash_defined 78 || h->root.type == bfd_link_hash_defweak) 79 && discarded_section (h->root.u.def.section)) 80 return h->root.u.def.section; 81 else 82 return NULL; 83 } 84 else 85 { 86 /* It's not a relocation against a global symbol, 87 but it could be a relocation against a local 88 symbol for a discarded section. */ 89 asection *isec; 90 Elf_Internal_Sym *isym; 91 92 /* Need to: get the symbol; get the section. */ 93 isym = &cookie->locsyms[r_symndx]; 94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx); 95 if (isec != NULL 96 && discard ? discarded_section (isec) : 1) 97 return isec; 98 } 99 return NULL; 100} 101 102/* Define a symbol in a dynamic linkage section. */ 103 104struct elf_link_hash_entry * 105_bfd_elf_define_linkage_sym (bfd *abfd, 106 struct bfd_link_info *info, 107 asection *sec, 108 const char *name) 109{ 110 struct elf_link_hash_entry *h; 111 struct bfd_link_hash_entry *bh; 112 const struct elf_backend_data *bed; 113 114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); 115 if (h != NULL) 116 { 117 /* Zap symbol defined in an as-needed lib that wasn't linked. 118 This is a symptom of a larger problem: Absolute symbols 119 defined in shared libraries can't be overridden, because we 120 lose the link to the bfd which is via the symbol section. */ 121 h->root.type = bfd_link_hash_new; 122 } 123 124 bh = &h->root; 125 bed = get_elf_backend_data (abfd); 126 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, 127 sec, 0, NULL, FALSE, bed->collect, 128 &bh)) 129 return NULL; 130 h = (struct elf_link_hash_entry *) bh; 131 h->def_regular = 1; 132 h->non_elf = 0; 133 h->root.linker_def = 1; 134 h->type = STT_OBJECT; 135 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) 136 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; 137 138 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 139 return h; 140} 141 142bfd_boolean 143_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) 144{ 145 flagword flags; 146 asection *s; 147 struct elf_link_hash_entry *h; 148 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 149 struct elf_link_hash_table *htab = elf_hash_table (info); 150 151 /* This function may be called more than once. */ 152 if (htab->sgot != NULL) 153 return TRUE; 154 155 flags = bed->dynamic_sec_flags; 156 157 s = bfd_make_section_anyway_with_flags (abfd, 158 (bed->rela_plts_and_copies_p 159 ? ".rela.got" : ".rel.got"), 160 (bed->dynamic_sec_flags 161 | SEC_READONLY)); 162 if (s == NULL 163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 164 return FALSE; 165 htab->srelgot = s; 166 167 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); 168 if (s == NULL 169 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 170 return FALSE; 171 htab->sgot = s; 172 173 if (bed->want_got_plt) 174 { 175 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); 176 if (s == NULL 177 || !bfd_set_section_alignment (abfd, s, 178 bed->s->log_file_align)) 179 return FALSE; 180 htab->sgotplt = s; 181 } 182 183 /* The first bit of the global offset table is the header. */ 184 s->size += bed->got_header_size; 185 186 if (bed->want_got_sym) 187 { 188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got 189 (or .got.plt) section. We don't do this in the linker script 190 because we don't want to define the symbol if we are not creating 191 a global offset table. */ 192 h = _bfd_elf_define_linkage_sym (abfd, info, s, 193 "_GLOBAL_OFFSET_TABLE_"); 194 elf_hash_table (info)->hgot = h; 195 if (h == NULL) 196 return FALSE; 197 } 198 199 return TRUE; 200} 201 202/* Create a strtab to hold the dynamic symbol names. */ 203static bfd_boolean 204_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) 205{ 206 struct elf_link_hash_table *hash_table; 207 208 hash_table = elf_hash_table (info); 209 if (hash_table->dynobj == NULL) 210 { 211 /* We may not set dynobj, an input file holding linker created 212 dynamic sections to abfd, which may be a dynamic object with 213 its own dynamic sections. We need to find a normal input file 214 to hold linker created sections if possible. */ 215 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0) 216 { 217 bfd *ibfd; 218 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next) 219 if ((ibfd->flags 220 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0) 221 { 222 abfd = ibfd; 223 break; 224 } 225 } 226 hash_table->dynobj = abfd; 227 } 228 229 if (hash_table->dynstr == NULL) 230 { 231 hash_table->dynstr = _bfd_elf_strtab_init (); 232 if (hash_table->dynstr == NULL) 233 return FALSE; 234 } 235 return TRUE; 236} 237 238/* Create some sections which will be filled in with dynamic linking 239 information. ABFD is an input file which requires dynamic sections 240 to be created. The dynamic sections take up virtual memory space 241 when the final executable is run, so we need to create them before 242 addresses are assigned to the output sections. We work out the 243 actual contents and size of these sections later. */ 244 245bfd_boolean 246_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 247{ 248 flagword flags; 249 asection *s; 250 const struct elf_backend_data *bed; 251 struct elf_link_hash_entry *h; 252 253 if (! is_elf_hash_table (info->hash)) 254 return FALSE; 255 256 if (elf_hash_table (info)->dynamic_sections_created) 257 return TRUE; 258 259 if (!_bfd_elf_link_create_dynstrtab (abfd, info)) 260 return FALSE; 261 262 abfd = elf_hash_table (info)->dynobj; 263 bed = get_elf_backend_data (abfd); 264 265 flags = bed->dynamic_sec_flags; 266 267 /* A dynamically linked executable has a .interp section, but a 268 shared library does not. */ 269 if (bfd_link_executable (info) && !info->nointerp) 270 { 271 s = bfd_make_section_anyway_with_flags (abfd, ".interp", 272 flags | SEC_READONLY); 273 if (s == NULL) 274 return FALSE; 275 } 276 277 /* Create sections to hold version informations. These are removed 278 if they are not needed. */ 279 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d", 280 flags | SEC_READONLY); 281 if (s == NULL 282 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 283 return FALSE; 284 285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version", 286 flags | SEC_READONLY); 287 if (s == NULL 288 || ! bfd_set_section_alignment (abfd, s, 1)) 289 return FALSE; 290 291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r", 292 flags | SEC_READONLY); 293 if (s == NULL 294 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 295 return FALSE; 296 297 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", 298 flags | SEC_READONLY); 299 if (s == NULL 300 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 301 return FALSE; 302 elf_hash_table (info)->dynsym = s; 303 304 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", 305 flags | SEC_READONLY); 306 if (s == NULL) 307 return FALSE; 308 309 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); 310 if (s == NULL 311 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 312 return FALSE; 313 314 /* The special symbol _DYNAMIC is always set to the start of the 315 .dynamic section. We could set _DYNAMIC in a linker script, but we 316 only want to define it if we are, in fact, creating a .dynamic 317 section. We don't want to define it if there is no .dynamic 318 section, since on some ELF platforms the start up code examines it 319 to decide how to initialize the process. */ 320 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"); 321 elf_hash_table (info)->hdynamic = h; 322 if (h == NULL) 323 return FALSE; 324 325 if (info->emit_hash) 326 { 327 s = bfd_make_section_anyway_with_flags (abfd, ".hash", 328 flags | SEC_READONLY); 329 if (s == NULL 330 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 331 return FALSE; 332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; 333 } 334 335 if (info->emit_gnu_hash) 336 { 337 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash", 338 flags | SEC_READONLY); 339 if (s == NULL 340 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 341 return FALSE; 342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: 343 4 32-bit words followed by variable count of 64-bit words, then 344 variable count of 32-bit words. */ 345 if (bed->s->arch_size == 64) 346 elf_section_data (s)->this_hdr.sh_entsize = 0; 347 else 348 elf_section_data (s)->this_hdr.sh_entsize = 4; 349 } 350 351 /* Let the backend create the rest of the sections. This lets the 352 backend set the right flags. The backend will normally create 353 the .got and .plt sections. */ 354 if (bed->elf_backend_create_dynamic_sections == NULL 355 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) 356 return FALSE; 357 358 elf_hash_table (info)->dynamic_sections_created = TRUE; 359 360 return TRUE; 361} 362 363/* Create dynamic sections when linking against a dynamic object. */ 364 365bfd_boolean 366_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 367{ 368 flagword flags, pltflags; 369 struct elf_link_hash_entry *h; 370 asection *s; 371 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 372 struct elf_link_hash_table *htab = elf_hash_table (info); 373 374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and 375 .rel[a].bss sections. */ 376 flags = bed->dynamic_sec_flags; 377 378 pltflags = flags; 379 if (bed->plt_not_loaded) 380 /* We do not clear SEC_ALLOC here because we still want the OS to 381 allocate space for the section; it's just that there's nothing 382 to read in from the object file. */ 383 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); 384 else 385 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; 386 if (bed->plt_readonly) 387 pltflags |= SEC_READONLY; 388 389 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); 390 if (s == NULL 391 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) 392 return FALSE; 393 htab->splt = s; 394 395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the 396 .plt section. */ 397 if (bed->want_plt_sym) 398 { 399 h = _bfd_elf_define_linkage_sym (abfd, info, s, 400 "_PROCEDURE_LINKAGE_TABLE_"); 401 elf_hash_table (info)->hplt = h; 402 if (h == NULL) 403 return FALSE; 404 } 405 406 s = bfd_make_section_anyway_with_flags (abfd, 407 (bed->rela_plts_and_copies_p 408 ? ".rela.plt" : ".rel.plt"), 409 flags | SEC_READONLY); 410 if (s == NULL 411 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 412 return FALSE; 413 htab->srelplt = s; 414 415 if (! _bfd_elf_create_got_section (abfd, info)) 416 return FALSE; 417 418 if (bed->want_dynbss) 419 { 420 /* The .dynbss section is a place to put symbols which are defined 421 by dynamic objects, are referenced by regular objects, and are 422 not functions. We must allocate space for them in the process 423 image and use a R_*_COPY reloc to tell the dynamic linker to 424 initialize them at run time. The linker script puts the .dynbss 425 section into the .bss section of the final image. */ 426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", 427 SEC_ALLOC | SEC_LINKER_CREATED); 428 if (s == NULL) 429 return FALSE; 430 htab->sdynbss = s; 431 432 if (bed->want_dynrelro) 433 { 434 /* Similarly, but for symbols that were originally in read-only 435 sections. This section doesn't really need to have contents, 436 but make it like other .data.rel.ro sections. */ 437 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro", 438 flags); 439 if (s == NULL) 440 return FALSE; 441 htab->sdynrelro = s; 442 } 443 444 /* The .rel[a].bss section holds copy relocs. This section is not 445 normally needed. We need to create it here, though, so that the 446 linker will map it to an output section. We can't just create it 447 only if we need it, because we will not know whether we need it 448 until we have seen all the input files, and the first time the 449 main linker code calls BFD after examining all the input files 450 (size_dynamic_sections) the input sections have already been 451 mapped to the output sections. If the section turns out not to 452 be needed, we can discard it later. We will never need this 453 section when generating a shared object, since they do not use 454 copy relocs. */ 455 if (bfd_link_executable (info)) 456 { 457 s = bfd_make_section_anyway_with_flags (abfd, 458 (bed->rela_plts_and_copies_p 459 ? ".rela.bss" : ".rel.bss"), 460 flags | SEC_READONLY); 461 if (s == NULL 462 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 463 return FALSE; 464 htab->srelbss = s; 465 466 if (bed->want_dynrelro) 467 { 468 s = (bfd_make_section_anyway_with_flags 469 (abfd, (bed->rela_plts_and_copies_p 470 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"), 471 flags | SEC_READONLY)); 472 if (s == NULL 473 || ! bfd_set_section_alignment (abfd, s, 474 bed->s->log_file_align)) 475 return FALSE; 476 htab->sreldynrelro = s; 477 } 478 } 479 } 480 481 return TRUE; 482} 483 484/* Record a new dynamic symbol. We record the dynamic symbols as we 485 read the input files, since we need to have a list of all of them 486 before we can determine the final sizes of the output sections. 487 Note that we may actually call this function even though we are not 488 going to output any dynamic symbols; in some cases we know that a 489 symbol should be in the dynamic symbol table, but only if there is 490 one. */ 491 492bfd_boolean 493bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, 494 struct elf_link_hash_entry *h) 495{ 496 if (h->dynindx == -1) 497 { 498 struct elf_strtab_hash *dynstr; 499 char *p; 500 const char *name; 501 size_t indx; 502 503 /* XXX: The ABI draft says the linker must turn hidden and 504 internal symbols into STB_LOCAL symbols when producing the 505 DSO. However, if ld.so honors st_other in the dynamic table, 506 this would not be necessary. */ 507 switch (ELF_ST_VISIBILITY (h->other)) 508 { 509 case STV_INTERNAL: 510 case STV_HIDDEN: 511 if (h->root.type != bfd_link_hash_undefined 512 && h->root.type != bfd_link_hash_undefweak) 513 { 514 h->forced_local = 1; 515 if (!elf_hash_table (info)->is_relocatable_executable) 516 return TRUE; 517 } 518 519 default: 520 break; 521 } 522 523 h->dynindx = elf_hash_table (info)->dynsymcount; 524 ++elf_hash_table (info)->dynsymcount; 525 526 dynstr = elf_hash_table (info)->dynstr; 527 if (dynstr == NULL) 528 { 529 /* Create a strtab to hold the dynamic symbol names. */ 530 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); 531 if (dynstr == NULL) 532 return FALSE; 533 } 534 535 /* We don't put any version information in the dynamic string 536 table. */ 537 name = h->root.root.string; 538 p = strchr (name, ELF_VER_CHR); 539 if (p != NULL) 540 /* We know that the p points into writable memory. In fact, 541 there are only a few symbols that have read-only names, being 542 those like _GLOBAL_OFFSET_TABLE_ that are created specially 543 by the backends. Most symbols will have names pointing into 544 an ELF string table read from a file, or to objalloc memory. */ 545 *p = 0; 546 547 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); 548 549 if (p != NULL) 550 *p = ELF_VER_CHR; 551 552 if (indx == (size_t) -1) 553 return FALSE; 554 h->dynstr_index = indx; 555 } 556 557 return TRUE; 558} 559 560/* Mark a symbol dynamic. */ 561 562static void 563bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, 564 struct elf_link_hash_entry *h, 565 Elf_Internal_Sym *sym) 566{ 567 struct bfd_elf_dynamic_list *d = info->dynamic_list; 568 569 /* It may be called more than once on the same H. */ 570 if(h->dynamic || bfd_link_relocatable (info)) 571 return; 572 573 if ((info->dynamic_data 574 && (h->type == STT_OBJECT 575 || h->type == STT_COMMON 576 || (sym != NULL 577 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT 578 || ELF_ST_TYPE (sym->st_info) == STT_COMMON)))) 579 || (d != NULL 580 && h->root.type == bfd_link_hash_new 581 && (*d->match) (&d->head, NULL, h->root.root.string))) 582 h->dynamic = 1; 583} 584 585/* Record an assignment to a symbol made by a linker script. We need 586 this in case some dynamic object refers to this symbol. */ 587 588bfd_boolean 589bfd_elf_record_link_assignment (bfd *output_bfd, 590 struct bfd_link_info *info, 591 const char *name, 592 bfd_boolean provide, 593 bfd_boolean hidden) 594{ 595 struct elf_link_hash_entry *h, *hv; 596 struct elf_link_hash_table *htab; 597 const struct elf_backend_data *bed; 598 599 if (!is_elf_hash_table (info->hash)) 600 return TRUE; 601 602 htab = elf_hash_table (info); 603 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); 604 if (h == NULL) 605 return provide; 606 607 if (h->root.type == bfd_link_hash_warning) 608 h = (struct elf_link_hash_entry *) h->root.u.i.link; 609 610 if (h->versioned == unknown) 611 { 612 /* Set versioned if symbol version is unknown. */ 613 char *version = strrchr (name, ELF_VER_CHR); 614 if (version) 615 { 616 if (version > name && version[-1] != ELF_VER_CHR) 617 h->versioned = versioned_hidden; 618 else 619 h->versioned = versioned; 620 } 621 } 622 623 switch (h->root.type) 624 { 625 case bfd_link_hash_defined: 626 case bfd_link_hash_defweak: 627 case bfd_link_hash_common: 628 break; 629 case bfd_link_hash_undefweak: 630 case bfd_link_hash_undefined: 631 /* Since we're defining the symbol, don't let it seem to have not 632 been defined. record_dynamic_symbol and size_dynamic_sections 633 may depend on this. */ 634 h->root.type = bfd_link_hash_new; 635 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) 636 bfd_link_repair_undef_list (&htab->root); 637 break; 638 case bfd_link_hash_new: 639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL); 640 h->non_elf = 0; 641 break; 642 case bfd_link_hash_indirect: 643 /* We had a versioned symbol in a dynamic library. We make the 644 the versioned symbol point to this one. */ 645 bed = get_elf_backend_data (output_bfd); 646 hv = h; 647 while (hv->root.type == bfd_link_hash_indirect 648 || hv->root.type == bfd_link_hash_warning) 649 hv = (struct elf_link_hash_entry *) hv->root.u.i.link; 650 /* We don't need to update h->root.u since linker will set them 651 later. */ 652 h->root.type = bfd_link_hash_undefined; 653 hv->root.type = bfd_link_hash_indirect; 654 hv->root.u.i.link = (struct bfd_link_hash_entry *) h; 655 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); 656 break; 657 default: 658 BFD_FAIL (); 659 return FALSE; 660 } 661 662 /* If this symbol is being provided by the linker script, and it is 663 currently defined by a dynamic object, but not by a regular 664 object, then mark it as undefined so that the generic linker will 665 force the correct value. */ 666 if (provide 667 && h->def_dynamic 668 && !h->def_regular) 669 h->root.type = bfd_link_hash_undefined; 670 671 /* If this symbol is not being provided by the linker script, and it is 672 currently defined by a dynamic object, but not by a regular object, 673 then undo any forced local marking that may have been set by input 674 section garbage collection and clear out any version information 675 because the symbol will not be associated with the dynamic object 676 any more. */ 677 if (!provide 678 && h->def_dynamic 679 && !h->def_regular) 680 { 681 h->forced_local = 0; 682 h->verinfo.verdef = NULL; 683 } 684 685 h->def_regular = 1; 686 687 if (hidden) 688 { 689 bed = get_elf_backend_data (output_bfd); 690 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) 691 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; 692 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 693 } 694 695 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects 696 and executables. */ 697 if (!bfd_link_relocatable (info) 698 && h->dynindx != -1 699 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN 700 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) 701 h->forced_local = 1; 702 703 if ((h->def_dynamic 704 || h->ref_dynamic 705 || bfd_link_dll (info) 706 || elf_hash_table (info)->is_relocatable_executable) 707 && h->dynindx == -1) 708 { 709 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 710 return FALSE; 711 712 /* If this is a weak defined symbol, and we know a corresponding 713 real symbol from the same dynamic object, make sure the real 714 symbol is also made into a dynamic symbol. */ 715 if (h->u.weakdef != NULL 716 && h->u.weakdef->dynindx == -1) 717 { 718 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) 719 return FALSE; 720 } 721 } 722 723 return TRUE; 724} 725 726/* Record a new local dynamic symbol. Returns 0 on failure, 1 on 727 success, and 2 on a failure caused by attempting to record a symbol 728 in a discarded section, eg. a discarded link-once section symbol. */ 729 730int 731bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, 732 bfd *input_bfd, 733 long input_indx) 734{ 735 bfd_size_type amt; 736 struct elf_link_local_dynamic_entry *entry; 737 struct elf_link_hash_table *eht; 738 struct elf_strtab_hash *dynstr; 739 size_t dynstr_index; 740 char *name; 741 Elf_External_Sym_Shndx eshndx; 742 char esym[sizeof (Elf64_External_Sym)]; 743 744 if (! is_elf_hash_table (info->hash)) 745 return 0; 746 747 /* See if the entry exists already. */ 748 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) 749 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) 750 return 1; 751 752 amt = sizeof (*entry); 753 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); 754 if (entry == NULL) 755 return 0; 756 757 /* Go find the symbol, so that we can find it's name. */ 758 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, 759 1, input_indx, &entry->isym, esym, &eshndx)) 760 { 761 bfd_release (input_bfd, entry); 762 return 0; 763 } 764 765 if (entry->isym.st_shndx != SHN_UNDEF 766 && entry->isym.st_shndx < SHN_LORESERVE) 767 { 768 asection *s; 769 770 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); 771 if (s == NULL || bfd_is_abs_section (s->output_section)) 772 { 773 /* We can still bfd_release here as nothing has done another 774 bfd_alloc. We can't do this later in this function. */ 775 bfd_release (input_bfd, entry); 776 return 2; 777 } 778 } 779 780 name = (bfd_elf_string_from_elf_section 781 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, 782 entry->isym.st_name)); 783 784 dynstr = elf_hash_table (info)->dynstr; 785 if (dynstr == NULL) 786 { 787 /* Create a strtab to hold the dynamic symbol names. */ 788 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); 789 if (dynstr == NULL) 790 return 0; 791 } 792 793 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); 794 if (dynstr_index == (size_t) -1) 795 return 0; 796 entry->isym.st_name = dynstr_index; 797 798 eht = elf_hash_table (info); 799 800 entry->next = eht->dynlocal; 801 eht->dynlocal = entry; 802 entry->input_bfd = input_bfd; 803 entry->input_indx = input_indx; 804 eht->dynsymcount++; 805 806 /* Whatever binding the symbol had before, it's now local. */ 807 entry->isym.st_info 808 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); 809 810 /* The dynindx will be set at the end of size_dynamic_sections. */ 811 812 return 1; 813} 814 815/* Return the dynindex of a local dynamic symbol. */ 816 817long 818_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, 819 bfd *input_bfd, 820 long input_indx) 821{ 822 struct elf_link_local_dynamic_entry *e; 823 824 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) 825 if (e->input_bfd == input_bfd && e->input_indx == input_indx) 826 return e->dynindx; 827 return -1; 828} 829 830/* This function is used to renumber the dynamic symbols, if some of 831 them are removed because they are marked as local. This is called 832 via elf_link_hash_traverse. */ 833 834static bfd_boolean 835elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, 836 void *data) 837{ 838 size_t *count = (size_t *) data; 839 840 if (h->forced_local) 841 return TRUE; 842 843 if (h->dynindx != -1) 844 h->dynindx = ++(*count); 845 846 return TRUE; 847} 848 849 850/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with 851 STB_LOCAL binding. */ 852 853static bfd_boolean 854elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, 855 void *data) 856{ 857 size_t *count = (size_t *) data; 858 859 if (!h->forced_local) 860 return TRUE; 861 862 if (h->dynindx != -1) 863 h->dynindx = ++(*count); 864 865 return TRUE; 866} 867 868/* Return true if the dynamic symbol for a given section should be 869 omitted when creating a shared library. */ 870bfd_boolean 871_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, 872 struct bfd_link_info *info, 873 asection *p) 874{ 875 struct elf_link_hash_table *htab; 876 asection *ip; 877 878 switch (elf_section_data (p)->this_hdr.sh_type) 879 { 880 case SHT_PROGBITS: 881 case SHT_NOBITS: 882 /* If sh_type is yet undecided, assume it could be 883 SHT_PROGBITS/SHT_NOBITS. */ 884 case SHT_NULL: 885 htab = elf_hash_table (info); 886 if (p == htab->tls_sec) 887 return FALSE; 888 889 if (htab->text_index_section != NULL) 890 return p != htab->text_index_section && p != htab->data_index_section; 891 892 return (htab->dynobj != NULL 893 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL 894 && ip->output_section == p); 895 896 /* There shouldn't be section relative relocations 897 against any other section. */ 898 default: 899 return TRUE; 900 } 901} 902 903/* Assign dynsym indices. In a shared library we generate a section 904 symbol for each output section, which come first. Next come symbols 905 which have been forced to local binding. Then all of the back-end 906 allocated local dynamic syms, followed by the rest of the global 907 symbols. */ 908 909static unsigned long 910_bfd_elf_link_renumber_dynsyms (bfd *output_bfd, 911 struct bfd_link_info *info, 912 unsigned long *section_sym_count) 913{ 914 unsigned long dynsymcount = 0; 915 916 if (bfd_link_pic (info) 917 || elf_hash_table (info)->is_relocatable_executable) 918 { 919 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 920 asection *p; 921 for (p = output_bfd->sections; p ; p = p->next) 922 if ((p->flags & SEC_EXCLUDE) == 0 923 && (p->flags & SEC_ALLOC) != 0 924 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) 925 elf_section_data (p)->dynindx = ++dynsymcount; 926 else 927 elf_section_data (p)->dynindx = 0; 928 } 929 *section_sym_count = dynsymcount; 930 931 elf_link_hash_traverse (elf_hash_table (info), 932 elf_link_renumber_local_hash_table_dynsyms, 933 &dynsymcount); 934 935 if (elf_hash_table (info)->dynlocal) 936 { 937 struct elf_link_local_dynamic_entry *p; 938 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) 939 p->dynindx = ++dynsymcount; 940 } 941 elf_hash_table (info)->local_dynsymcount = dynsymcount; 942 943 elf_link_hash_traverse (elf_hash_table (info), 944 elf_link_renumber_hash_table_dynsyms, 945 &dynsymcount); 946 947 /* There is an unused NULL entry at the head of the table which we 948 must account for in our count even if the table is empty since it 949 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in 950 .dynamic section. */ 951 dynsymcount++; 952 953 elf_hash_table (info)->dynsymcount = dynsymcount; 954 return dynsymcount; 955} 956 957/* Merge st_other field. */ 958 959static void 960elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, 961 const Elf_Internal_Sym *isym, asection *sec, 962 bfd_boolean definition, bfd_boolean dynamic) 963{ 964 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 965 966 /* If st_other has a processor-specific meaning, specific 967 code might be needed here. */ 968 if (bed->elf_backend_merge_symbol_attribute) 969 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, 970 dynamic); 971 972 if (!dynamic) 973 { 974 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); 975 unsigned hvis = ELF_ST_VISIBILITY (h->other); 976 977 /* Keep the most constraining visibility. Leave the remainder 978 of the st_other field to elf_backend_merge_symbol_attribute. */ 979 if (symvis - 1 < hvis - 1) 980 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); 981 } 982 else if (definition 983 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT 984 && (sec->flags & SEC_READONLY) == 0) 985 h->protected_def = 1; 986} 987 988/* This function is called when we want to merge a new symbol with an 989 existing symbol. It handles the various cases which arise when we 990 find a definition in a dynamic object, or when there is already a 991 definition in a dynamic object. The new symbol is described by 992 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table 993 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK 994 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment 995 of an old common symbol. We set OVERRIDE if the old symbol is 996 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for 997 the type to change. We set SIZE_CHANGE_OK if it is OK for the size 998 to change. By OK to change, we mean that we shouldn't warn if the 999 type or size does change. */ 1000 1001static bfd_boolean 1002_bfd_elf_merge_symbol (bfd *abfd, 1003 struct bfd_link_info *info, 1004 const char *name, 1005 Elf_Internal_Sym *sym, 1006 asection **psec, 1007 bfd_vma *pvalue, 1008 struct elf_link_hash_entry **sym_hash, 1009 bfd **poldbfd, 1010 bfd_boolean *pold_weak, 1011 unsigned int *pold_alignment, 1012 bfd_boolean *skip, 1013 bfd_boolean *override, 1014 bfd_boolean *type_change_ok, 1015 bfd_boolean *size_change_ok, 1016 bfd_boolean *matched) 1017{ 1018 asection *sec, *oldsec; 1019 struct elf_link_hash_entry *h; 1020 struct elf_link_hash_entry *hi; 1021 struct elf_link_hash_entry *flip; 1022 int bind; 1023 bfd *oldbfd; 1024 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; 1025 bfd_boolean newweak, oldweak, newfunc, oldfunc; 1026 const struct elf_backend_data *bed; 1027 char *new_version; 1028 1029 *skip = FALSE; 1030 *override = FALSE; 1031 1032 sec = *psec; 1033 bind = ELF_ST_BIND (sym->st_info); 1034 1035 if (! bfd_is_und_section (sec)) 1036 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); 1037 else 1038 h = ((struct elf_link_hash_entry *) 1039 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); 1040 if (h == NULL) 1041 return FALSE; 1042 *sym_hash = h; 1043 1044 bed = get_elf_backend_data (abfd); 1045 1046 /* NEW_VERSION is the symbol version of the new symbol. */ 1047 if (h->versioned != unversioned) 1048 { 1049 /* Symbol version is unknown or versioned. */ 1050 new_version = strrchr (name, ELF_VER_CHR); 1051 if (new_version) 1052 { 1053 if (h->versioned == unknown) 1054 { 1055 if (new_version > name && new_version[-1] != ELF_VER_CHR) 1056 h->versioned = versioned_hidden; 1057 else 1058 h->versioned = versioned; 1059 } 1060 new_version += 1; 1061 if (new_version[0] == '\0') 1062 new_version = NULL; 1063 } 1064 else 1065 h->versioned = unversioned; 1066 } 1067 else 1068 new_version = NULL; 1069 1070 /* For merging, we only care about real symbols. But we need to make 1071 sure that indirect symbol dynamic flags are updated. */ 1072 hi = h; 1073 while (h->root.type == bfd_link_hash_indirect 1074 || h->root.type == bfd_link_hash_warning) 1075 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1076 1077 if (!*matched) 1078 { 1079 if (hi == h || h->root.type == bfd_link_hash_new) 1080 *matched = TRUE; 1081 else 1082 { 1083 /* OLD_HIDDEN is true if the existing symbol is only visible 1084 to the symbol with the same symbol version. NEW_HIDDEN is 1085 true if the new symbol is only visible to the symbol with 1086 the same symbol version. */ 1087 bfd_boolean old_hidden = h->versioned == versioned_hidden; 1088 bfd_boolean new_hidden = hi->versioned == versioned_hidden; 1089 if (!old_hidden && !new_hidden) 1090 /* The new symbol matches the existing symbol if both 1091 aren't hidden. */ 1092 *matched = TRUE; 1093 else 1094 { 1095 /* OLD_VERSION is the symbol version of the existing 1096 symbol. */ 1097 char *old_version; 1098 1099 if (h->versioned >= versioned) 1100 old_version = strrchr (h->root.root.string, 1101 ELF_VER_CHR) + 1; 1102 else 1103 old_version = NULL; 1104 1105 /* The new symbol matches the existing symbol if they 1106 have the same symbol version. */ 1107 *matched = (old_version == new_version 1108 || (old_version != NULL 1109 && new_version != NULL 1110 && strcmp (old_version, new_version) == 0)); 1111 } 1112 } 1113 } 1114 1115 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the 1116 existing symbol. */ 1117 1118 oldbfd = NULL; 1119 oldsec = NULL; 1120 switch (h->root.type) 1121 { 1122 default: 1123 break; 1124 1125 case bfd_link_hash_undefined: 1126 case bfd_link_hash_undefweak: 1127 oldbfd = h->root.u.undef.abfd; 1128 break; 1129 1130 case bfd_link_hash_defined: 1131 case bfd_link_hash_defweak: 1132 oldbfd = h->root.u.def.section->owner; 1133 oldsec = h->root.u.def.section; 1134 break; 1135 1136 case bfd_link_hash_common: 1137 oldbfd = h->root.u.c.p->section->owner; 1138 oldsec = h->root.u.c.p->section; 1139 if (pold_alignment) 1140 *pold_alignment = h->root.u.c.p->alignment_power; 1141 break; 1142 } 1143 if (poldbfd && *poldbfd == NULL) 1144 *poldbfd = oldbfd; 1145 1146 /* Differentiate strong and weak symbols. */ 1147 newweak = bind == STB_WEAK; 1148 oldweak = (h->root.type == bfd_link_hash_defweak 1149 || h->root.type == bfd_link_hash_undefweak); 1150 if (pold_weak) 1151 *pold_weak = oldweak; 1152 1153 /* This code is for coping with dynamic objects, and is only useful 1154 if we are doing an ELF link. */ 1155 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) 1156 return TRUE; 1157 1158 /* We have to check it for every instance since the first few may be 1159 references and not all compilers emit symbol type for undefined 1160 symbols. */ 1161 bfd_elf_link_mark_dynamic_symbol (info, h, sym); 1162 1163 /* NEWDYN and OLDDYN indicate whether the new or old symbol, 1164 respectively, is from a dynamic object. */ 1165 1166 newdyn = (abfd->flags & DYNAMIC) != 0; 1167 1168 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined 1169 syms and defined syms in dynamic libraries respectively. 1170 ref_dynamic on the other hand can be set for a symbol defined in 1171 a dynamic library, and def_dynamic may not be set; When the 1172 definition in a dynamic lib is overridden by a definition in the 1173 executable use of the symbol in the dynamic lib becomes a 1174 reference to the executable symbol. */ 1175 if (newdyn) 1176 { 1177 if (bfd_is_und_section (sec)) 1178 { 1179 if (bind != STB_WEAK) 1180 { 1181 h->ref_dynamic_nonweak = 1; 1182 hi->ref_dynamic_nonweak = 1; 1183 } 1184 } 1185 else 1186 { 1187 /* Update the existing symbol only if they match. */ 1188 if (*matched) 1189 h->dynamic_def = 1; 1190 hi->dynamic_def = 1; 1191 } 1192 } 1193 1194 /* If we just created the symbol, mark it as being an ELF symbol. 1195 Other than that, there is nothing to do--there is no merge issue 1196 with a newly defined symbol--so we just return. */ 1197 1198 if (h->root.type == bfd_link_hash_new) 1199 { 1200 h->non_elf = 0; 1201 return TRUE; 1202 } 1203 1204 /* In cases involving weak versioned symbols, we may wind up trying 1205 to merge a symbol with itself. Catch that here, to avoid the 1206 confusion that results if we try to override a symbol with 1207 itself. The additional tests catch cases like 1208 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a 1209 dynamic object, which we do want to handle here. */ 1210 if (abfd == oldbfd 1211 && (newweak || oldweak) 1212 && ((abfd->flags & DYNAMIC) == 0 1213 || !h->def_regular)) 1214 return TRUE; 1215 1216 olddyn = FALSE; 1217 if (oldbfd != NULL) 1218 olddyn = (oldbfd->flags & DYNAMIC) != 0; 1219 else if (oldsec != NULL) 1220 { 1221 /* This handles the special SHN_MIPS_{TEXT,DATA} section 1222 indices used by MIPS ELF. */ 1223 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; 1224 } 1225 1226 /* NEWDEF and OLDDEF indicate whether the new or old symbol, 1227 respectively, appear to be a definition rather than reference. */ 1228 1229 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); 1230 1231 olddef = (h->root.type != bfd_link_hash_undefined 1232 && h->root.type != bfd_link_hash_undefweak 1233 && h->root.type != bfd_link_hash_common); 1234 1235 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, 1236 respectively, appear to be a function. */ 1237 1238 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE 1239 && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); 1240 1241 oldfunc = (h->type != STT_NOTYPE 1242 && bed->is_function_type (h->type)); 1243 1244 /* If creating a default indirect symbol ("foo" or "foo@") from a 1245 dynamic versioned definition ("foo@@") skip doing so if there is 1246 an existing regular definition with a different type. We don't 1247 want, for example, a "time" variable in the executable overriding 1248 a "time" function in a shared library. */ 1249 if (pold_alignment == NULL 1250 && newdyn 1251 && newdef 1252 && !olddyn 1253 && (olddef || h->root.type == bfd_link_hash_common) 1254 && ELF_ST_TYPE (sym->st_info) != h->type 1255 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE 1256 && h->type != STT_NOTYPE 1257 && !(newfunc && oldfunc)) 1258 { 1259 *skip = TRUE; 1260 return TRUE; 1261 } 1262 1263 /* Check TLS symbols. We don't check undefined symbols introduced 1264 by "ld -u" which have no type (and oldbfd NULL), and we don't 1265 check symbols from plugins because they also have no type. */ 1266 if (oldbfd != NULL 1267 && (oldbfd->flags & BFD_PLUGIN) == 0 1268 && (abfd->flags & BFD_PLUGIN) == 0 1269 && ELF_ST_TYPE (sym->st_info) != h->type 1270 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) 1271 { 1272 bfd *ntbfd, *tbfd; 1273 bfd_boolean ntdef, tdef; 1274 asection *ntsec, *tsec; 1275 1276 if (h->type == STT_TLS) 1277 { 1278 ntbfd = abfd; 1279 ntsec = sec; 1280 ntdef = newdef; 1281 tbfd = oldbfd; 1282 tsec = oldsec; 1283 tdef = olddef; 1284 } 1285 else 1286 { 1287 ntbfd = oldbfd; 1288 ntsec = oldsec; 1289 ntdef = olddef; 1290 tbfd = abfd; 1291 tsec = sec; 1292 tdef = newdef; 1293 } 1294 1295 if (tdef && ntdef) 1296 _bfd_error_handler 1297 /* xgettext:c-format */ 1298 (_("%s: TLS definition in %B section %A " 1299 "mismatches non-TLS definition in %B section %A"), 1300 tbfd, tsec, ntbfd, ntsec, h->root.root.string); 1301 else if (!tdef && !ntdef) 1302 _bfd_error_handler 1303 /* xgettext:c-format */ 1304 (_("%s: TLS reference in %B " 1305 "mismatches non-TLS reference in %B"), 1306 tbfd, ntbfd, h->root.root.string); 1307 else if (tdef) 1308 _bfd_error_handler 1309 /* xgettext:c-format */ 1310 (_("%s: TLS definition in %B section %A " 1311 "mismatches non-TLS reference in %B"), 1312 tbfd, tsec, ntbfd, h->root.root.string); 1313 else 1314 _bfd_error_handler 1315 /* xgettext:c-format */ 1316 (_("%s: TLS reference in %B " 1317 "mismatches non-TLS definition in %B section %A"), 1318 tbfd, ntbfd, ntsec, h->root.root.string); 1319 1320 bfd_set_error (bfd_error_bad_value); 1321 return FALSE; 1322 } 1323 1324 /* If the old symbol has non-default visibility, we ignore the new 1325 definition from a dynamic object. */ 1326 if (newdyn 1327 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 1328 && !bfd_is_und_section (sec)) 1329 { 1330 *skip = TRUE; 1331 /* Make sure this symbol is dynamic. */ 1332 h->ref_dynamic = 1; 1333 hi->ref_dynamic = 1; 1334 /* A protected symbol has external availability. Make sure it is 1335 recorded as dynamic. 1336 1337 FIXME: Should we check type and size for protected symbol? */ 1338 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) 1339 return bfd_elf_link_record_dynamic_symbol (info, h); 1340 else 1341 return TRUE; 1342 } 1343 else if (!newdyn 1344 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT 1345 && h->def_dynamic) 1346 { 1347 /* If the new symbol with non-default visibility comes from a 1348 relocatable file and the old definition comes from a dynamic 1349 object, we remove the old definition. */ 1350 if (hi->root.type == bfd_link_hash_indirect) 1351 { 1352 /* Handle the case where the old dynamic definition is 1353 default versioned. We need to copy the symbol info from 1354 the symbol with default version to the normal one if it 1355 was referenced before. */ 1356 if (h->ref_regular) 1357 { 1358 hi->root.type = h->root.type; 1359 h->root.type = bfd_link_hash_indirect; 1360 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); 1361 1362 h->root.u.i.link = (struct bfd_link_hash_entry *) hi; 1363 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) 1364 { 1365 /* If the new symbol is hidden or internal, completely undo 1366 any dynamic link state. */ 1367 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1368 h->forced_local = 0; 1369 h->ref_dynamic = 0; 1370 } 1371 else 1372 h->ref_dynamic = 1; 1373 1374 h->def_dynamic = 0; 1375 /* FIXME: Should we check type and size for protected symbol? */ 1376 h->size = 0; 1377 h->type = 0; 1378 1379 h = hi; 1380 } 1381 else 1382 h = hi; 1383 } 1384 1385 /* If the old symbol was undefined before, then it will still be 1386 on the undefs list. If the new symbol is undefined or 1387 common, we can't make it bfd_link_hash_new here, because new 1388 undefined or common symbols will be added to the undefs list 1389 by _bfd_generic_link_add_one_symbol. Symbols may not be 1390 added twice to the undefs list. Also, if the new symbol is 1391 undefweak then we don't want to lose the strong undef. */ 1392 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) 1393 { 1394 h->root.type = bfd_link_hash_undefined; 1395 h->root.u.undef.abfd = abfd; 1396 } 1397 else 1398 { 1399 h->root.type = bfd_link_hash_new; 1400 h->root.u.undef.abfd = NULL; 1401 } 1402 1403 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) 1404 { 1405 /* If the new symbol is hidden or internal, completely undo 1406 any dynamic link state. */ 1407 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1408 h->forced_local = 0; 1409 h->ref_dynamic = 0; 1410 } 1411 else 1412 h->ref_dynamic = 1; 1413 h->def_dynamic = 0; 1414 /* FIXME: Should we check type and size for protected symbol? */ 1415 h->size = 0; 1416 h->type = 0; 1417 return TRUE; 1418 } 1419 1420 /* If a new weak symbol definition comes from a regular file and the 1421 old symbol comes from a dynamic library, we treat the new one as 1422 strong. Similarly, an old weak symbol definition from a regular 1423 file is treated as strong when the new symbol comes from a dynamic 1424 library. Further, an old weak symbol from a dynamic library is 1425 treated as strong if the new symbol is from a dynamic library. 1426 This reflects the way glibc's ld.so works. 1427 1428 Do this before setting *type_change_ok or *size_change_ok so that 1429 we warn properly when dynamic library symbols are overridden. */ 1430 1431 if (newdef && !newdyn && olddyn) 1432 newweak = FALSE; 1433 if (olddef && newdyn) 1434 oldweak = FALSE; 1435 1436 /* Allow changes between different types of function symbol. */ 1437 if (newfunc && oldfunc) 1438 *type_change_ok = TRUE; 1439 1440 /* It's OK to change the type if either the existing symbol or the 1441 new symbol is weak. A type change is also OK if the old symbol 1442 is undefined and the new symbol is defined. */ 1443 1444 if (oldweak 1445 || newweak 1446 || (newdef 1447 && h->root.type == bfd_link_hash_undefined)) 1448 *type_change_ok = TRUE; 1449 1450 /* It's OK to change the size if either the existing symbol or the 1451 new symbol is weak, or if the old symbol is undefined. */ 1452 1453 if (*type_change_ok 1454 || h->root.type == bfd_link_hash_undefined) 1455 *size_change_ok = TRUE; 1456 1457 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old 1458 symbol, respectively, appears to be a common symbol in a dynamic 1459 object. If a symbol appears in an uninitialized section, and is 1460 not weak, and is not a function, then it may be a common symbol 1461 which was resolved when the dynamic object was created. We want 1462 to treat such symbols specially, because they raise special 1463 considerations when setting the symbol size: if the symbol 1464 appears as a common symbol in a regular object, and the size in 1465 the regular object is larger, we must make sure that we use the 1466 larger size. This problematic case can always be avoided in C, 1467 but it must be handled correctly when using Fortran shared 1468 libraries. 1469 1470 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and 1471 likewise for OLDDYNCOMMON and OLDDEF. 1472 1473 Note that this test is just a heuristic, and that it is quite 1474 possible to have an uninitialized symbol in a shared object which 1475 is really a definition, rather than a common symbol. This could 1476 lead to some minor confusion when the symbol really is a common 1477 symbol in some regular object. However, I think it will be 1478 harmless. */ 1479 1480 if (newdyn 1481 && newdef 1482 && !newweak 1483 && (sec->flags & SEC_ALLOC) != 0 1484 && (sec->flags & SEC_LOAD) == 0 1485 && sym->st_size > 0 1486 && !newfunc) 1487 newdyncommon = TRUE; 1488 else 1489 newdyncommon = FALSE; 1490 1491 if (olddyn 1492 && olddef 1493 && h->root.type == bfd_link_hash_defined 1494 && h->def_dynamic 1495 && (h->root.u.def.section->flags & SEC_ALLOC) != 0 1496 && (h->root.u.def.section->flags & SEC_LOAD) == 0 1497 && h->size > 0 1498 && !oldfunc) 1499 olddyncommon = TRUE; 1500 else 1501 olddyncommon = FALSE; 1502 1503 /* We now know everything about the old and new symbols. We ask the 1504 backend to check if we can merge them. */ 1505 if (bed->merge_symbol != NULL) 1506 { 1507 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) 1508 return FALSE; 1509 sec = *psec; 1510 } 1511 1512 /* If both the old and the new symbols look like common symbols in a 1513 dynamic object, set the size of the symbol to the larger of the 1514 two. */ 1515 1516 if (olddyncommon 1517 && newdyncommon 1518 && sym->st_size != h->size) 1519 { 1520 /* Since we think we have two common symbols, issue a multiple 1521 common warning if desired. Note that we only warn if the 1522 size is different. If the size is the same, we simply let 1523 the old symbol override the new one as normally happens with 1524 symbols defined in dynamic objects. */ 1525 1526 (*info->callbacks->multiple_common) (info, &h->root, abfd, 1527 bfd_link_hash_common, sym->st_size); 1528 if (sym->st_size > h->size) 1529 h->size = sym->st_size; 1530 1531 *size_change_ok = TRUE; 1532 } 1533 1534 /* If we are looking at a dynamic object, and we have found a 1535 definition, we need to see if the symbol was already defined by 1536 some other object. If so, we want to use the existing 1537 definition, and we do not want to report a multiple symbol 1538 definition error; we do this by clobbering *PSEC to be 1539 bfd_und_section_ptr. 1540 1541 We treat a common symbol as a definition if the symbol in the 1542 shared library is a function, since common symbols always 1543 represent variables; this can cause confusion in principle, but 1544 any such confusion would seem to indicate an erroneous program or 1545 shared library. We also permit a common symbol in a regular 1546 object to override a weak symbol in a shared object. */ 1547 1548 if (newdyn 1549 && newdef 1550 && (olddef 1551 || (h->root.type == bfd_link_hash_common 1552 && (newweak || newfunc)))) 1553 { 1554 *override = TRUE; 1555 newdef = FALSE; 1556 newdyncommon = FALSE; 1557 1558 *psec = sec = bfd_und_section_ptr; 1559 *size_change_ok = TRUE; 1560 1561 /* If we get here when the old symbol is a common symbol, then 1562 we are explicitly letting it override a weak symbol or 1563 function in a dynamic object, and we don't want to warn about 1564 a type change. If the old symbol is a defined symbol, a type 1565 change warning may still be appropriate. */ 1566 1567 if (h->root.type == bfd_link_hash_common) 1568 *type_change_ok = TRUE; 1569 } 1570 1571 /* Handle the special case of an old common symbol merging with a 1572 new symbol which looks like a common symbol in a shared object. 1573 We change *PSEC and *PVALUE to make the new symbol look like a 1574 common symbol, and let _bfd_generic_link_add_one_symbol do the 1575 right thing. */ 1576 1577 if (newdyncommon 1578 && h->root.type == bfd_link_hash_common) 1579 { 1580 *override = TRUE; 1581 newdef = FALSE; 1582 newdyncommon = FALSE; 1583 *pvalue = sym->st_size; 1584 *psec = sec = bed->common_section (oldsec); 1585 *size_change_ok = TRUE; 1586 } 1587 1588 /* Skip weak definitions of symbols that are already defined. */ 1589 if (newdef && olddef && newweak) 1590 { 1591 /* Don't skip new non-IR weak syms. */ 1592 if (!(oldbfd != NULL 1593 && (oldbfd->flags & BFD_PLUGIN) != 0 1594 && (abfd->flags & BFD_PLUGIN) == 0)) 1595 { 1596 newdef = FALSE; 1597 *skip = TRUE; 1598 } 1599 1600 /* Merge st_other. If the symbol already has a dynamic index, 1601 but visibility says it should not be visible, turn it into a 1602 local symbol. */ 1603 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); 1604 if (h->dynindx != -1) 1605 switch (ELF_ST_VISIBILITY (h->other)) 1606 { 1607 case STV_INTERNAL: 1608 case STV_HIDDEN: 1609 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1610 break; 1611 } 1612 } 1613 1614 /* If the old symbol is from a dynamic object, and the new symbol is 1615 a definition which is not from a dynamic object, then the new 1616 symbol overrides the old symbol. Symbols from regular files 1617 always take precedence over symbols from dynamic objects, even if 1618 they are defined after the dynamic object in the link. 1619 1620 As above, we again permit a common symbol in a regular object to 1621 override a definition in a shared object if the shared object 1622 symbol is a function or is weak. */ 1623 1624 flip = NULL; 1625 if (!newdyn 1626 && (newdef 1627 || (bfd_is_com_section (sec) 1628 && (oldweak || oldfunc))) 1629 && olddyn 1630 && olddef 1631 && h->def_dynamic) 1632 { 1633 /* Change the hash table entry to undefined, and let 1634 _bfd_generic_link_add_one_symbol do the right thing with the 1635 new definition. */ 1636 1637 h->root.type = bfd_link_hash_undefined; 1638 h->root.u.undef.abfd = h->root.u.def.section->owner; 1639 *size_change_ok = TRUE; 1640 1641 olddef = FALSE; 1642 olddyncommon = FALSE; 1643 1644 /* We again permit a type change when a common symbol may be 1645 overriding a function. */ 1646 1647 if (bfd_is_com_section (sec)) 1648 { 1649 if (oldfunc) 1650 { 1651 /* If a common symbol overrides a function, make sure 1652 that it isn't defined dynamically nor has type 1653 function. */ 1654 h->def_dynamic = 0; 1655 h->type = STT_NOTYPE; 1656 } 1657 *type_change_ok = TRUE; 1658 } 1659 1660 if (hi->root.type == bfd_link_hash_indirect) 1661 flip = hi; 1662 else 1663 /* This union may have been set to be non-NULL when this symbol 1664 was seen in a dynamic object. We must force the union to be 1665 NULL, so that it is correct for a regular symbol. */ 1666 h->verinfo.vertree = NULL; 1667 } 1668 1669 /* Handle the special case of a new common symbol merging with an 1670 old symbol that looks like it might be a common symbol defined in 1671 a shared object. Note that we have already handled the case in 1672 which a new common symbol should simply override the definition 1673 in the shared library. */ 1674 1675 if (! newdyn 1676 && bfd_is_com_section (sec) 1677 && olddyncommon) 1678 { 1679 /* It would be best if we could set the hash table entry to a 1680 common symbol, but we don't know what to use for the section 1681 or the alignment. */ 1682 (*info->callbacks->multiple_common) (info, &h->root, abfd, 1683 bfd_link_hash_common, sym->st_size); 1684 1685 /* If the presumed common symbol in the dynamic object is 1686 larger, pretend that the new symbol has its size. */ 1687 1688 if (h->size > *pvalue) 1689 *pvalue = h->size; 1690 1691 /* We need to remember the alignment required by the symbol 1692 in the dynamic object. */ 1693 BFD_ASSERT (pold_alignment); 1694 *pold_alignment = h->root.u.def.section->alignment_power; 1695 1696 olddef = FALSE; 1697 olddyncommon = FALSE; 1698 1699 h->root.type = bfd_link_hash_undefined; 1700 h->root.u.undef.abfd = h->root.u.def.section->owner; 1701 1702 *size_change_ok = TRUE; 1703 *type_change_ok = TRUE; 1704 1705 if (hi->root.type == bfd_link_hash_indirect) 1706 flip = hi; 1707 else 1708 h->verinfo.vertree = NULL; 1709 } 1710 1711 if (flip != NULL) 1712 { 1713 /* Handle the case where we had a versioned symbol in a dynamic 1714 library and now find a definition in a normal object. In this 1715 case, we make the versioned symbol point to the normal one. */ 1716 flip->root.type = h->root.type; 1717 flip->root.u.undef.abfd = h->root.u.undef.abfd; 1718 h->root.type = bfd_link_hash_indirect; 1719 h->root.u.i.link = (struct bfd_link_hash_entry *) flip; 1720 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); 1721 if (h->def_dynamic) 1722 { 1723 h->def_dynamic = 0; 1724 flip->ref_dynamic = 1; 1725 } 1726 } 1727 1728 return TRUE; 1729} 1730 1731/* This function is called to create an indirect symbol from the 1732 default for the symbol with the default version if needed. The 1733 symbol is described by H, NAME, SYM, SEC, and VALUE. We 1734 set DYNSYM if the new indirect symbol is dynamic. */ 1735 1736static bfd_boolean 1737_bfd_elf_add_default_symbol (bfd *abfd, 1738 struct bfd_link_info *info, 1739 struct elf_link_hash_entry *h, 1740 const char *name, 1741 Elf_Internal_Sym *sym, 1742 asection *sec, 1743 bfd_vma value, 1744 bfd **poldbfd, 1745 bfd_boolean *dynsym) 1746{ 1747 bfd_boolean type_change_ok; 1748 bfd_boolean size_change_ok; 1749 bfd_boolean skip; 1750 char *shortname; 1751 struct elf_link_hash_entry *hi; 1752 struct bfd_link_hash_entry *bh; 1753 const struct elf_backend_data *bed; 1754 bfd_boolean collect; 1755 bfd_boolean dynamic; 1756 bfd_boolean override; 1757 char *p; 1758 size_t len, shortlen; 1759 asection *tmp_sec; 1760 bfd_boolean matched; 1761 1762 if (h->versioned == unversioned || h->versioned == versioned_hidden) 1763 return TRUE; 1764 1765 /* If this symbol has a version, and it is the default version, we 1766 create an indirect symbol from the default name to the fully 1767 decorated name. This will cause external references which do not 1768 specify a version to be bound to this version of the symbol. */ 1769 p = strchr (name, ELF_VER_CHR); 1770 if (h->versioned == unknown) 1771 { 1772 if (p == NULL) 1773 { 1774 h->versioned = unversioned; 1775 return TRUE; 1776 } 1777 else 1778 { 1779 if (p[1] != ELF_VER_CHR) 1780 { 1781 h->versioned = versioned_hidden; 1782 return TRUE; 1783 } 1784 else 1785 h->versioned = versioned; 1786 } 1787 } 1788 else 1789 { 1790 /* PR ld/19073: We may see an unversioned definition after the 1791 default version. */ 1792 if (p == NULL) 1793 return TRUE; 1794 } 1795 1796 bed = get_elf_backend_data (abfd); 1797 collect = bed->collect; 1798 dynamic = (abfd->flags & DYNAMIC) != 0; 1799 1800 shortlen = p - name; 1801 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); 1802 if (shortname == NULL) 1803 return FALSE; 1804 memcpy (shortname, name, shortlen); 1805 shortname[shortlen] = '\0'; 1806 1807 /* We are going to create a new symbol. Merge it with any existing 1808 symbol with this name. For the purposes of the merge, act as 1809 though we were defining the symbol we just defined, although we 1810 actually going to define an indirect symbol. */ 1811 type_change_ok = FALSE; 1812 size_change_ok = FALSE; 1813 matched = TRUE; 1814 tmp_sec = sec; 1815 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, 1816 &hi, poldbfd, NULL, NULL, &skip, &override, 1817 &type_change_ok, &size_change_ok, &matched)) 1818 return FALSE; 1819 1820 if (skip) 1821 goto nondefault; 1822 1823 if (hi->def_regular) 1824 { 1825 /* If the undecorated symbol will have a version added by a 1826 script different to H, then don't indirect to/from the 1827 undecorated symbol. This isn't ideal because we may not yet 1828 have seen symbol versions, if given by a script on the 1829 command line rather than via --version-script. */ 1830 if (hi->verinfo.vertree == NULL && info->version_info != NULL) 1831 { 1832 bfd_boolean hide; 1833 1834 hi->verinfo.vertree 1835 = bfd_find_version_for_sym (info->version_info, 1836 hi->root.root.string, &hide); 1837 if (hi->verinfo.vertree != NULL && hide) 1838 { 1839 (*bed->elf_backend_hide_symbol) (info, hi, TRUE); 1840 goto nondefault; 1841 } 1842 } 1843 if (hi->verinfo.vertree != NULL 1844 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) 1845 goto nondefault; 1846 } 1847 1848 if (! override) 1849 { 1850 /* Add the default symbol if not performing a relocatable link. */ 1851 if (! bfd_link_relocatable (info)) 1852 { 1853 bh = &hi->root; 1854 if (! (_bfd_generic_link_add_one_symbol 1855 (info, abfd, shortname, BSF_INDIRECT, 1856 bfd_ind_section_ptr, 1857 0, name, FALSE, collect, &bh))) 1858 return FALSE; 1859 hi = (struct elf_link_hash_entry *) bh; 1860 } 1861 } 1862 else 1863 { 1864 /* In this case the symbol named SHORTNAME is overriding the 1865 indirect symbol we want to add. We were planning on making 1866 SHORTNAME an indirect symbol referring to NAME. SHORTNAME 1867 is the name without a version. NAME is the fully versioned 1868 name, and it is the default version. 1869 1870 Overriding means that we already saw a definition for the 1871 symbol SHORTNAME in a regular object, and it is overriding 1872 the symbol defined in the dynamic object. 1873 1874 When this happens, we actually want to change NAME, the 1875 symbol we just added, to refer to SHORTNAME. This will cause 1876 references to NAME in the shared object to become references 1877 to SHORTNAME in the regular object. This is what we expect 1878 when we override a function in a shared object: that the 1879 references in the shared object will be mapped to the 1880 definition in the regular object. */ 1881 1882 while (hi->root.type == bfd_link_hash_indirect 1883 || hi->root.type == bfd_link_hash_warning) 1884 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 1885 1886 h->root.type = bfd_link_hash_indirect; 1887 h->root.u.i.link = (struct bfd_link_hash_entry *) hi; 1888 if (h->def_dynamic) 1889 { 1890 h->def_dynamic = 0; 1891 hi->ref_dynamic = 1; 1892 if (hi->ref_regular 1893 || hi->def_regular) 1894 { 1895 if (! bfd_elf_link_record_dynamic_symbol (info, hi)) 1896 return FALSE; 1897 } 1898 } 1899 1900 /* Now set HI to H, so that the following code will set the 1901 other fields correctly. */ 1902 hi = h; 1903 } 1904 1905 /* Check if HI is a warning symbol. */ 1906 if (hi->root.type == bfd_link_hash_warning) 1907 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 1908 1909 /* If there is a duplicate definition somewhere, then HI may not 1910 point to an indirect symbol. We will have reported an error to 1911 the user in that case. */ 1912 1913 if (hi->root.type == bfd_link_hash_indirect) 1914 { 1915 struct elf_link_hash_entry *ht; 1916 1917 ht = (struct elf_link_hash_entry *) hi->root.u.i.link; 1918 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); 1919 1920 /* A reference to the SHORTNAME symbol from a dynamic library 1921 will be satisfied by the versioned symbol at runtime. In 1922 effect, we have a reference to the versioned symbol. */ 1923 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; 1924 hi->dynamic_def |= ht->dynamic_def; 1925 1926 /* See if the new flags lead us to realize that the symbol must 1927 be dynamic. */ 1928 if (! *dynsym) 1929 { 1930 if (! dynamic) 1931 { 1932 if (! bfd_link_executable (info) 1933 || hi->def_dynamic 1934 || hi->ref_dynamic) 1935 *dynsym = TRUE; 1936 } 1937 else 1938 { 1939 if (hi->ref_regular) 1940 *dynsym = TRUE; 1941 } 1942 } 1943 } 1944 1945 /* We also need to define an indirection from the nondefault version 1946 of the symbol. */ 1947 1948nondefault: 1949 len = strlen (name); 1950 shortname = (char *) bfd_hash_allocate (&info->hash->table, len); 1951 if (shortname == NULL) 1952 return FALSE; 1953 memcpy (shortname, name, shortlen); 1954 memcpy (shortname + shortlen, p + 1, len - shortlen); 1955 1956 /* Once again, merge with any existing symbol. */ 1957 type_change_ok = FALSE; 1958 size_change_ok = FALSE; 1959 tmp_sec = sec; 1960 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, 1961 &hi, poldbfd, NULL, NULL, &skip, &override, 1962 &type_change_ok, &size_change_ok, &matched)) 1963 return FALSE; 1964 1965 if (skip) 1966 return TRUE; 1967 1968 if (override) 1969 { 1970 /* Here SHORTNAME is a versioned name, so we don't expect to see 1971 the type of override we do in the case above unless it is 1972 overridden by a versioned definition. */ 1973 if (hi->root.type != bfd_link_hash_defined 1974 && hi->root.type != bfd_link_hash_defweak) 1975 _bfd_error_handler 1976 /* xgettext:c-format */ 1977 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), 1978 abfd, shortname); 1979 } 1980 else 1981 { 1982 bh = &hi->root; 1983 if (! (_bfd_generic_link_add_one_symbol 1984 (info, abfd, shortname, BSF_INDIRECT, 1985 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) 1986 return FALSE; 1987 hi = (struct elf_link_hash_entry *) bh; 1988 1989 /* If there is a duplicate definition somewhere, then HI may not 1990 point to an indirect symbol. We will have reported an error 1991 to the user in that case. */ 1992 1993 if (hi->root.type == bfd_link_hash_indirect) 1994 { 1995 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); 1996 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; 1997 hi->dynamic_def |= h->dynamic_def; 1998 1999 /* See if the new flags lead us to realize that the symbol 2000 must be dynamic. */ 2001 if (! *dynsym) 2002 { 2003 if (! dynamic) 2004 { 2005 if (! bfd_link_executable (info) 2006 || hi->ref_dynamic) 2007 *dynsym = TRUE; 2008 } 2009 else 2010 { 2011 if (hi->ref_regular) 2012 *dynsym = TRUE; 2013 } 2014 } 2015 } 2016 } 2017 2018 return TRUE; 2019} 2020 2021/* This routine is used to export all defined symbols into the dynamic 2022 symbol table. It is called via elf_link_hash_traverse. */ 2023 2024static bfd_boolean 2025_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) 2026{ 2027 struct elf_info_failed *eif = (struct elf_info_failed *) data; 2028 2029 /* Ignore indirect symbols. These are added by the versioning code. */ 2030 if (h->root.type == bfd_link_hash_indirect) 2031 return TRUE; 2032 2033 /* Ignore this if we won't export it. */ 2034 if (!eif->info->export_dynamic && !h->dynamic) 2035 return TRUE; 2036 2037 if (h->dynindx == -1 2038 && (h->def_regular || h->ref_regular) 2039 && ! bfd_hide_sym_by_version (eif->info->version_info, 2040 h->root.root.string)) 2041 { 2042 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) 2043 { 2044 eif->failed = TRUE; 2045 return FALSE; 2046 } 2047 } 2048 2049 return TRUE; 2050} 2051 2052/* Look through the symbols which are defined in other shared 2053 libraries and referenced here. Update the list of version 2054 dependencies. This will be put into the .gnu.version_r section. 2055 This function is called via elf_link_hash_traverse. */ 2056 2057static bfd_boolean 2058_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, 2059 void *data) 2060{ 2061 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; 2062 Elf_Internal_Verneed *t; 2063 Elf_Internal_Vernaux *a; 2064 bfd_size_type amt; 2065 2066 /* We only care about symbols defined in shared objects with version 2067 information. */ 2068 if (!h->def_dynamic 2069 || h->def_regular 2070 || h->dynindx == -1 2071 || h->verinfo.verdef == NULL 2072 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) 2073 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) 2074 return TRUE; 2075 2076 /* See if we already know about this version. */ 2077 for (t = elf_tdata (rinfo->info->output_bfd)->verref; 2078 t != NULL; 2079 t = t->vn_nextref) 2080 { 2081 if (t->vn_bfd != h->verinfo.verdef->vd_bfd) 2082 continue; 2083 2084 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 2085 if (a->vna_nodename == h->verinfo.verdef->vd_nodename) 2086 return TRUE; 2087 2088 break; 2089 } 2090 2091 /* This is a new version. Add it to tree we are building. */ 2092 2093 if (t == NULL) 2094 { 2095 amt = sizeof *t; 2096 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); 2097 if (t == NULL) 2098 { 2099 rinfo->failed = TRUE; 2100 return FALSE; 2101 } 2102 2103 t->vn_bfd = h->verinfo.verdef->vd_bfd; 2104 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; 2105 elf_tdata (rinfo->info->output_bfd)->verref = t; 2106 } 2107 2108 amt = sizeof *a; 2109 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); 2110 if (a == NULL) 2111 { 2112 rinfo->failed = TRUE; 2113 return FALSE; 2114 } 2115 2116 /* Note that we are copying a string pointer here, and testing it 2117 above. If bfd_elf_string_from_elf_section is ever changed to 2118 discard the string data when low in memory, this will have to be 2119 fixed. */ 2120 a->vna_nodename = h->verinfo.verdef->vd_nodename; 2121 2122 a->vna_flags = h->verinfo.verdef->vd_flags; 2123 a->vna_nextptr = t->vn_auxptr; 2124 2125 h->verinfo.verdef->vd_exp_refno = rinfo->vers; 2126 ++rinfo->vers; 2127 2128 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; 2129 2130 t->vn_auxptr = a; 2131 2132 return TRUE; 2133} 2134 2135/* Figure out appropriate versions for all the symbols. We may not 2136 have the version number script until we have read all of the input 2137 files, so until that point we don't know which symbols should be 2138 local. This function is called via elf_link_hash_traverse. */ 2139 2140static bfd_boolean 2141_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) 2142{ 2143 struct elf_info_failed *sinfo; 2144 struct bfd_link_info *info; 2145 const struct elf_backend_data *bed; 2146 struct elf_info_failed eif; 2147 char *p; 2148 2149 sinfo = (struct elf_info_failed *) data; 2150 info = sinfo->info; 2151 2152 /* Fix the symbol flags. */ 2153 eif.failed = FALSE; 2154 eif.info = info; 2155 if (! _bfd_elf_fix_symbol_flags (h, &eif)) 2156 { 2157 if (eif.failed) 2158 sinfo->failed = TRUE; 2159 return FALSE; 2160 } 2161 2162 /* We only need version numbers for symbols defined in regular 2163 objects. */ 2164 if (!h->def_regular) 2165 return TRUE; 2166 2167 bed = get_elf_backend_data (info->output_bfd); 2168 p = strchr (h->root.root.string, ELF_VER_CHR); 2169 if (p != NULL && h->verinfo.vertree == NULL) 2170 { 2171 struct bfd_elf_version_tree *t; 2172 2173 ++p; 2174 if (*p == ELF_VER_CHR) 2175 ++p; 2176 2177 /* If there is no version string, we can just return out. */ 2178 if (*p == '\0') 2179 return TRUE; 2180 2181 /* Look for the version. If we find it, it is no longer weak. */ 2182 for (t = sinfo->info->version_info; t != NULL; t = t->next) 2183 { 2184 if (strcmp (t->name, p) == 0) 2185 { 2186 size_t len; 2187 char *alc; 2188 struct bfd_elf_version_expr *d; 2189 2190 len = p - h->root.root.string; 2191 alc = (char *) bfd_malloc (len); 2192 if (alc == NULL) 2193 { 2194 sinfo->failed = TRUE; 2195 return FALSE; 2196 } 2197 memcpy (alc, h->root.root.string, len - 1); 2198 alc[len - 1] = '\0'; 2199 if (alc[len - 2] == ELF_VER_CHR) 2200 alc[len - 2] = '\0'; 2201 2202 h->verinfo.vertree = t; 2203 t->used = TRUE; 2204 d = NULL; 2205 2206 if (t->globals.list != NULL) 2207 d = (*t->match) (&t->globals, NULL, alc); 2208 2209 /* See if there is anything to force this symbol to 2210 local scope. */ 2211 if (d == NULL && t->locals.list != NULL) 2212 { 2213 d = (*t->match) (&t->locals, NULL, alc); 2214 if (d != NULL 2215 && h->dynindx != -1 2216 && ! info->export_dynamic) 2217 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 2218 } 2219 2220 free (alc); 2221 break; 2222 } 2223 } 2224 2225 /* If we are building an application, we need to create a 2226 version node for this version. */ 2227 if (t == NULL && bfd_link_executable (info)) 2228 { 2229 struct bfd_elf_version_tree **pp; 2230 int version_index; 2231 2232 /* If we aren't going to export this symbol, we don't need 2233 to worry about it. */ 2234 if (h->dynindx == -1) 2235 return TRUE; 2236 2237 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, 2238 sizeof *t); 2239 if (t == NULL) 2240 { 2241 sinfo->failed = TRUE; 2242 return FALSE; 2243 } 2244 2245 t->name = p; 2246 t->name_indx = (unsigned int) -1; 2247 t->used = TRUE; 2248 2249 version_index = 1; 2250 /* Don't count anonymous version tag. */ 2251 if (sinfo->info->version_info != NULL 2252 && sinfo->info->version_info->vernum == 0) 2253 version_index = 0; 2254 for (pp = &sinfo->info->version_info; 2255 *pp != NULL; 2256 pp = &(*pp)->next) 2257 ++version_index; 2258 t->vernum = version_index; 2259 2260 *pp = t; 2261 2262 h->verinfo.vertree = t; 2263 } 2264 else if (t == NULL) 2265 { 2266 /* We could not find the version for a symbol when 2267 generating a shared archive. Return an error. */ 2268 _bfd_error_handler 2269 /* xgettext:c-format */ 2270 (_("%B: version node not found for symbol %s"), 2271 info->output_bfd, h->root.root.string); 2272 bfd_set_error (bfd_error_bad_value); 2273 sinfo->failed = TRUE; 2274 return FALSE; 2275 } 2276 } 2277 2278 /* If we don't have a version for this symbol, see if we can find 2279 something. */ 2280 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL) 2281 { 2282 bfd_boolean hide; 2283 2284 h->verinfo.vertree 2285 = bfd_find_version_for_sym (sinfo->info->version_info, 2286 h->root.root.string, &hide); 2287 if (h->verinfo.vertree != NULL && hide) 2288 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 2289 } 2290 2291 return TRUE; 2292} 2293 2294/* Read and swap the relocs from the section indicated by SHDR. This 2295 may be either a REL or a RELA section. The relocations are 2296 translated into RELA relocations and stored in INTERNAL_RELOCS, 2297 which should have already been allocated to contain enough space. 2298 The EXTERNAL_RELOCS are a buffer where the external form of the 2299 relocations should be stored. 2300 2301 Returns FALSE if something goes wrong. */ 2302 2303static bfd_boolean 2304elf_link_read_relocs_from_section (bfd *abfd, 2305 asection *sec, 2306 Elf_Internal_Shdr *shdr, 2307 void *external_relocs, 2308 Elf_Internal_Rela *internal_relocs) 2309{ 2310 const struct elf_backend_data *bed; 2311 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 2312 const bfd_byte *erela; 2313 const bfd_byte *erelaend; 2314 Elf_Internal_Rela *irela; 2315 Elf_Internal_Shdr *symtab_hdr; 2316 size_t nsyms; 2317 2318 /* Position ourselves at the start of the section. */ 2319 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) 2320 return FALSE; 2321 2322 /* Read the relocations. */ 2323 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) 2324 return FALSE; 2325 2326 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2327 nsyms = NUM_SHDR_ENTRIES (symtab_hdr); 2328 2329 bed = get_elf_backend_data (abfd); 2330 2331 /* Convert the external relocations to the internal format. */ 2332 if (shdr->sh_entsize == bed->s->sizeof_rel) 2333 swap_in = bed->s->swap_reloc_in; 2334 else if (shdr->sh_entsize == bed->s->sizeof_rela) 2335 swap_in = bed->s->swap_reloca_in; 2336 else 2337 { 2338 bfd_set_error (bfd_error_wrong_format); 2339 return FALSE; 2340 } 2341 2342 erela = (const bfd_byte *) external_relocs; 2343 erelaend = erela + shdr->sh_size; 2344 irela = internal_relocs; 2345 while (erela < erelaend) 2346 { 2347 bfd_vma r_symndx; 2348 2349 (*swap_in) (abfd, erela, irela); 2350 r_symndx = ELF32_R_SYM (irela->r_info); 2351 if (bed->s->arch_size == 64) 2352 r_symndx >>= 24; 2353 if (nsyms > 0) 2354 { 2355 if ((size_t) r_symndx >= nsyms) 2356 { 2357 _bfd_error_handler 2358 /* xgettext:c-format */ 2359 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" 2360 " for offset 0x%lx in section `%A'"), 2361 abfd, sec, 2362 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); 2363 bfd_set_error (bfd_error_bad_value); 2364 return FALSE; 2365 } 2366 } 2367 else if (r_symndx != STN_UNDEF) 2368 { 2369 _bfd_error_handler 2370 /* xgettext:c-format */ 2371 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'" 2372 " when the object file has no symbol table"), 2373 abfd, sec, 2374 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); 2375 bfd_set_error (bfd_error_bad_value); 2376 return FALSE; 2377 } 2378 irela += bed->s->int_rels_per_ext_rel; 2379 erela += shdr->sh_entsize; 2380 } 2381 2382 return TRUE; 2383} 2384 2385/* Read and swap the relocs for a section O. They may have been 2386 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are 2387 not NULL, they are used as buffers to read into. They are known to 2388 be large enough. If the INTERNAL_RELOCS relocs argument is NULL, 2389 the return value is allocated using either malloc or bfd_alloc, 2390 according to the KEEP_MEMORY argument. If O has two relocation 2391 sections (both REL and RELA relocations), then the REL_HDR 2392 relocations will appear first in INTERNAL_RELOCS, followed by the 2393 RELA_HDR relocations. */ 2394 2395Elf_Internal_Rela * 2396_bfd_elf_link_read_relocs (bfd *abfd, 2397 asection *o, 2398 void *external_relocs, 2399 Elf_Internal_Rela *internal_relocs, 2400 bfd_boolean keep_memory) 2401{ 2402 void *alloc1 = NULL; 2403 Elf_Internal_Rela *alloc2 = NULL; 2404 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 2405 struct bfd_elf_section_data *esdo = elf_section_data (o); 2406 Elf_Internal_Rela *internal_rela_relocs; 2407 2408 if (esdo->relocs != NULL) 2409 return esdo->relocs; 2410 2411 if (o->reloc_count == 0) 2412 return NULL; 2413 2414 if (internal_relocs == NULL) 2415 { 2416 bfd_size_type size; 2417 2418 size = o->reloc_count; 2419 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); 2420 if (keep_memory) 2421 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); 2422 else 2423 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); 2424 if (internal_relocs == NULL) 2425 goto error_return; 2426 } 2427 2428 if (external_relocs == NULL) 2429 { 2430 bfd_size_type size = 0; 2431 2432 if (esdo->rel.hdr) 2433 size += esdo->rel.hdr->sh_size; 2434 if (esdo->rela.hdr) 2435 size += esdo->rela.hdr->sh_size; 2436 2437 alloc1 = bfd_malloc (size); 2438 if (alloc1 == NULL) 2439 goto error_return; 2440 external_relocs = alloc1; 2441 } 2442 2443 internal_rela_relocs = internal_relocs; 2444 if (esdo->rel.hdr) 2445 { 2446 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, 2447 external_relocs, 2448 internal_relocs)) 2449 goto error_return; 2450 external_relocs = (((bfd_byte *) external_relocs) 2451 + esdo->rel.hdr->sh_size); 2452 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) 2453 * bed->s->int_rels_per_ext_rel); 2454 } 2455 2456 if (esdo->rela.hdr 2457 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, 2458 external_relocs, 2459 internal_rela_relocs))) 2460 goto error_return; 2461 2462 /* Cache the results for next time, if we can. */ 2463 if (keep_memory) 2464 esdo->relocs = internal_relocs; 2465 2466 if (alloc1 != NULL) 2467 free (alloc1); 2468 2469 /* Don't free alloc2, since if it was allocated we are passing it 2470 back (under the name of internal_relocs). */ 2471 2472 return internal_relocs; 2473 2474 error_return: 2475 if (alloc1 != NULL) 2476 free (alloc1); 2477 if (alloc2 != NULL) 2478 { 2479 if (keep_memory) 2480 bfd_release (abfd, alloc2); 2481 else 2482 free (alloc2); 2483 } 2484 return NULL; 2485} 2486 2487/* Compute the size of, and allocate space for, REL_HDR which is the 2488 section header for a section containing relocations for O. */ 2489 2490static bfd_boolean 2491_bfd_elf_link_size_reloc_section (bfd *abfd, 2492 struct bfd_elf_section_reloc_data *reldata) 2493{ 2494 Elf_Internal_Shdr *rel_hdr = reldata->hdr; 2495 2496 /* That allows us to calculate the size of the section. */ 2497 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; 2498 2499 /* The contents field must last into write_object_contents, so we 2500 allocate it with bfd_alloc rather than malloc. Also since we 2501 cannot be sure that the contents will actually be filled in, 2502 we zero the allocated space. */ 2503 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); 2504 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) 2505 return FALSE; 2506 2507 if (reldata->hashes == NULL && reldata->count) 2508 { 2509 struct elf_link_hash_entry **p; 2510 2511 p = ((struct elf_link_hash_entry **) 2512 bfd_zmalloc (reldata->count * sizeof (*p))); 2513 if (p == NULL) 2514 return FALSE; 2515 2516 reldata->hashes = p; 2517 } 2518 2519 return TRUE; 2520} 2521 2522/* Copy the relocations indicated by the INTERNAL_RELOCS (which 2523 originated from the section given by INPUT_REL_HDR) to the 2524 OUTPUT_BFD. */ 2525 2526bfd_boolean 2527_bfd_elf_link_output_relocs (bfd *output_bfd, 2528 asection *input_section, 2529 Elf_Internal_Shdr *input_rel_hdr, 2530 Elf_Internal_Rela *internal_relocs, 2531 struct elf_link_hash_entry **rel_hash 2532 ATTRIBUTE_UNUSED) 2533{ 2534 Elf_Internal_Rela *irela; 2535 Elf_Internal_Rela *irelaend; 2536 bfd_byte *erel; 2537 struct bfd_elf_section_reloc_data *output_reldata; 2538 asection *output_section; 2539 const struct elf_backend_data *bed; 2540 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 2541 struct bfd_elf_section_data *esdo; 2542 2543 output_section = input_section->output_section; 2544 2545 bed = get_elf_backend_data (output_bfd); 2546 esdo = elf_section_data (output_section); 2547 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) 2548 { 2549 output_reldata = &esdo->rel; 2550 swap_out = bed->s->swap_reloc_out; 2551 } 2552 else if (esdo->rela.hdr 2553 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) 2554 { 2555 output_reldata = &esdo->rela; 2556 swap_out = bed->s->swap_reloca_out; 2557 } 2558 else 2559 { 2560 _bfd_error_handler 2561 /* xgettext:c-format */ 2562 (_("%B: relocation size mismatch in %B section %A"), 2563 output_bfd, input_section->owner, input_section); 2564 bfd_set_error (bfd_error_wrong_format); 2565 return FALSE; 2566 } 2567 2568 erel = output_reldata->hdr->contents; 2569 erel += output_reldata->count * input_rel_hdr->sh_entsize; 2570 irela = internal_relocs; 2571 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) 2572 * bed->s->int_rels_per_ext_rel); 2573 while (irela < irelaend) 2574 { 2575 (*swap_out) (output_bfd, irela, erel); 2576 irela += bed->s->int_rels_per_ext_rel; 2577 erel += input_rel_hdr->sh_entsize; 2578 } 2579 2580 /* Bump the counter, so that we know where to add the next set of 2581 relocations. */ 2582 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); 2583 2584 return TRUE; 2585} 2586 2587/* Make weak undefined symbols in PIE dynamic. */ 2588 2589bfd_boolean 2590_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, 2591 struct elf_link_hash_entry *h) 2592{ 2593 if (bfd_link_pie (info) 2594 && h->dynindx == -1 2595 && h->root.type == bfd_link_hash_undefweak) 2596 return bfd_elf_link_record_dynamic_symbol (info, h); 2597 2598 return TRUE; 2599} 2600 2601/* Fix up the flags for a symbol. This handles various cases which 2602 can only be fixed after all the input files are seen. This is 2603 currently called by both adjust_dynamic_symbol and 2604 assign_sym_version, which is unnecessary but perhaps more robust in 2605 the face of future changes. */ 2606 2607static bfd_boolean 2608_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, 2609 struct elf_info_failed *eif) 2610{ 2611 const struct elf_backend_data *bed; 2612 2613 /* If this symbol was mentioned in a non-ELF file, try to set 2614 DEF_REGULAR and REF_REGULAR correctly. This is the only way to 2615 permit a non-ELF file to correctly refer to a symbol defined in 2616 an ELF dynamic object. */ 2617 if (h->non_elf) 2618 { 2619 while (h->root.type == bfd_link_hash_indirect) 2620 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2621 2622 if (h->root.type != bfd_link_hash_defined 2623 && h->root.type != bfd_link_hash_defweak) 2624 { 2625 h->ref_regular = 1; 2626 h->ref_regular_nonweak = 1; 2627 } 2628 else 2629 { 2630 if (h->root.u.def.section->owner != NULL 2631 && (bfd_get_flavour (h->root.u.def.section->owner) 2632 == bfd_target_elf_flavour)) 2633 { 2634 h->ref_regular = 1; 2635 h->ref_regular_nonweak = 1; 2636 } 2637 else 2638 h->def_regular = 1; 2639 } 2640 2641 if (h->dynindx == -1 2642 && (h->def_dynamic 2643 || h->ref_dynamic)) 2644 { 2645 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) 2646 { 2647 eif->failed = TRUE; 2648 return FALSE; 2649 } 2650 } 2651 } 2652 else 2653 { 2654 /* Unfortunately, NON_ELF is only correct if the symbol 2655 was first seen in a non-ELF file. Fortunately, if the symbol 2656 was first seen in an ELF file, we're probably OK unless the 2657 symbol was defined in a non-ELF file. Catch that case here. 2658 FIXME: We're still in trouble if the symbol was first seen in 2659 a dynamic object, and then later in a non-ELF regular object. */ 2660 if ((h->root.type == bfd_link_hash_defined 2661 || h->root.type == bfd_link_hash_defweak) 2662 && !h->def_regular 2663 && (h->root.u.def.section->owner != NULL 2664 ? (bfd_get_flavour (h->root.u.def.section->owner) 2665 != bfd_target_elf_flavour) 2666 : (bfd_is_abs_section (h->root.u.def.section) 2667 && !h->def_dynamic))) 2668 h->def_regular = 1; 2669 } 2670 2671 /* Backend specific symbol fixup. */ 2672 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); 2673 if (bed->elf_backend_fixup_symbol 2674 && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) 2675 return FALSE; 2676 2677 /* If this is a final link, and the symbol was defined as a common 2678 symbol in a regular object file, and there was no definition in 2679 any dynamic object, then the linker will have allocated space for 2680 the symbol in a common section but the DEF_REGULAR 2681 flag will not have been set. */ 2682 if (h->root.type == bfd_link_hash_defined 2683 && !h->def_regular 2684 && h->ref_regular 2685 && !h->def_dynamic 2686 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) 2687 h->def_regular = 1; 2688 2689 /* If a weak undefined symbol has non-default visibility, we also 2690 hide it from the dynamic linker. */ 2691 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 2692 && h->root.type == bfd_link_hash_undefweak) 2693 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); 2694 2695 /* A hidden versioned symbol in executable should be forced local if 2696 it is is locally defined, not referenced by shared library and not 2697 exported. */ 2698 else if (bfd_link_executable (eif->info) 2699 && h->versioned == versioned_hidden 2700 && !eif->info->export_dynamic 2701 && !h->dynamic 2702 && !h->ref_dynamic 2703 && h->def_regular) 2704 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); 2705 2706 /* If -Bsymbolic was used (which means to bind references to global 2707 symbols to the definition within the shared object), and this 2708 symbol was defined in a regular object, then it actually doesn't 2709 need a PLT entry. Likewise, if the symbol has non-default 2710 visibility. If the symbol has hidden or internal visibility, we 2711 will force it local. */ 2712 else if (h->needs_plt 2713 && bfd_link_pic (eif->info) 2714 && is_elf_hash_table (eif->info->hash) 2715 && (SYMBOLIC_BIND (eif->info, h) 2716 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 2717 && h->def_regular) 2718 { 2719 bfd_boolean force_local; 2720 2721 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL 2722 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); 2723 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); 2724 } 2725 2726 /* If this is a weak defined symbol in a dynamic object, and we know 2727 the real definition in the dynamic object, copy interesting flags 2728 over to the real definition. */ 2729 if (h->u.weakdef != NULL) 2730 { 2731 /* If the real definition is defined by a regular object file, 2732 don't do anything special. See the longer description in 2733 _bfd_elf_adjust_dynamic_symbol, below. */ 2734 if (h->u.weakdef->def_regular) 2735 h->u.weakdef = NULL; 2736 else 2737 { 2738 struct elf_link_hash_entry *weakdef = h->u.weakdef; 2739 2740 while (h->root.type == bfd_link_hash_indirect) 2741 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2742 2743 BFD_ASSERT (h->root.type == bfd_link_hash_defined 2744 || h->root.type == bfd_link_hash_defweak); 2745 BFD_ASSERT (weakdef->def_dynamic); 2746 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined 2747 || weakdef->root.type == bfd_link_hash_defweak); 2748 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h); 2749 } 2750 } 2751 2752 return TRUE; 2753} 2754 2755/* Make the backend pick a good value for a dynamic symbol. This is 2756 called via elf_link_hash_traverse, and also calls itself 2757 recursively. */ 2758 2759static bfd_boolean 2760_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) 2761{ 2762 struct elf_info_failed *eif = (struct elf_info_failed *) data; 2763 bfd *dynobj; 2764 const struct elf_backend_data *bed; 2765 2766 if (! is_elf_hash_table (eif->info->hash)) 2767 return FALSE; 2768 2769 /* Ignore indirect symbols. These are added by the versioning code. */ 2770 if (h->root.type == bfd_link_hash_indirect) 2771 return TRUE; 2772 2773 /* Fix the symbol flags. */ 2774 if (! _bfd_elf_fix_symbol_flags (h, eif)) 2775 return FALSE; 2776 2777 /* If this symbol does not require a PLT entry, and it is not 2778 defined by a dynamic object, or is not referenced by a regular 2779 object, ignore it. We do have to handle a weak defined symbol, 2780 even if no regular object refers to it, if we decided to add it 2781 to the dynamic symbol table. FIXME: Do we normally need to worry 2782 about symbols which are defined by one dynamic object and 2783 referenced by another one? */ 2784 if (!h->needs_plt 2785 && h->type != STT_GNU_IFUNC 2786 && (h->def_regular 2787 || !h->def_dynamic 2788 || (!h->ref_regular 2789 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) 2790 { 2791 h->plt = elf_hash_table (eif->info)->init_plt_offset; 2792 return TRUE; 2793 } 2794 2795 /* If we've already adjusted this symbol, don't do it again. This 2796 can happen via a recursive call. */ 2797 if (h->dynamic_adjusted) 2798 return TRUE; 2799 2800 /* Don't look at this symbol again. Note that we must set this 2801 after checking the above conditions, because we may look at a 2802 symbol once, decide not to do anything, and then get called 2803 recursively later after REF_REGULAR is set below. */ 2804 h->dynamic_adjusted = 1; 2805 2806 /* If this is a weak definition, and we know a real definition, and 2807 the real symbol is not itself defined by a regular object file, 2808 then get a good value for the real definition. We handle the 2809 real symbol first, for the convenience of the backend routine. 2810 2811 Note that there is a confusing case here. If the real definition 2812 is defined by a regular object file, we don't get the real symbol 2813 from the dynamic object, but we do get the weak symbol. If the 2814 processor backend uses a COPY reloc, then if some routine in the 2815 dynamic object changes the real symbol, we will not see that 2816 change in the corresponding weak symbol. This is the way other 2817 ELF linkers work as well, and seems to be a result of the shared 2818 library model. 2819 2820 I will clarify this issue. Most SVR4 shared libraries define the 2821 variable _timezone and define timezone as a weak synonym. The 2822 tzset call changes _timezone. If you write 2823 extern int timezone; 2824 int _timezone = 5; 2825 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } 2826 you might expect that, since timezone is a synonym for _timezone, 2827 the same number will print both times. However, if the processor 2828 backend uses a COPY reloc, then actually timezone will be copied 2829 into your process image, and, since you define _timezone 2830 yourself, _timezone will not. Thus timezone and _timezone will 2831 wind up at different memory locations. The tzset call will set 2832 _timezone, leaving timezone unchanged. */ 2833 2834 if (h->u.weakdef != NULL) 2835 { 2836 /* If we get to this point, there is an implicit reference to 2837 H->U.WEAKDEF by a regular object file via the weak symbol H. */ 2838 h->u.weakdef->ref_regular = 1; 2839 2840 /* Ensure that the backend adjust_dynamic_symbol function sees 2841 H->U.WEAKDEF before H by recursively calling ourselves. */ 2842 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) 2843 return FALSE; 2844 } 2845 2846 /* If a symbol has no type and no size and does not require a PLT 2847 entry, then we are probably about to do the wrong thing here: we 2848 are probably going to create a COPY reloc for an empty object. 2849 This case can arise when a shared object is built with assembly 2850 code, and the assembly code fails to set the symbol type. */ 2851 if (h->size == 0 2852 && h->type == STT_NOTYPE 2853 && !h->needs_plt) 2854 _bfd_error_handler 2855 (_("warning: type and size of dynamic symbol `%s' are not defined"), 2856 h->root.root.string); 2857 2858 dynobj = elf_hash_table (eif->info)->dynobj; 2859 bed = get_elf_backend_data (dynobj); 2860 2861 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) 2862 { 2863 eif->failed = TRUE; 2864 return FALSE; 2865 } 2866 2867 return TRUE; 2868} 2869 2870/* Adjust the dynamic symbol, H, for copy in the dynamic bss section, 2871 DYNBSS. */ 2872 2873bfd_boolean 2874_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, 2875 struct elf_link_hash_entry *h, 2876 asection *dynbss) 2877{ 2878 unsigned int power_of_two; 2879 bfd_vma mask; 2880 asection *sec = h->root.u.def.section; 2881 2882 /* The section aligment of definition is the maximum alignment 2883 requirement of symbols defined in the section. Since we don't 2884 know the symbol alignment requirement, we start with the 2885 maximum alignment and check low bits of the symbol address 2886 for the minimum alignment. */ 2887 power_of_two = bfd_get_section_alignment (sec->owner, sec); 2888 mask = ((bfd_vma) 1 << power_of_two) - 1; 2889 while ((h->root.u.def.value & mask) != 0) 2890 { 2891 mask >>= 1; 2892 --power_of_two; 2893 } 2894 2895 if (power_of_two > bfd_get_section_alignment (dynbss->owner, 2896 dynbss)) 2897 { 2898 /* Adjust the section alignment if needed. */ 2899 if (! bfd_set_section_alignment (dynbss->owner, dynbss, 2900 power_of_two)) 2901 return FALSE; 2902 } 2903 2904 /* We make sure that the symbol will be aligned properly. */ 2905 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); 2906 2907 /* Define the symbol as being at this point in DYNBSS. */ 2908 h->root.u.def.section = dynbss; 2909 h->root.u.def.value = dynbss->size; 2910 2911 /* Increment the size of DYNBSS to make room for the symbol. */ 2912 dynbss->size += h->size; 2913 2914 /* No error if extern_protected_data is true. */ 2915 if (h->protected_def 2916 && (!info->extern_protected_data 2917 || (info->extern_protected_data < 0 2918 && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) 2919 info->callbacks->einfo 2920 (_("%P: copy reloc against protected `%T' is dangerous\n"), 2921 h->root.root.string); 2922 2923 return TRUE; 2924} 2925 2926/* Adjust all external symbols pointing into SEC_MERGE sections 2927 to reflect the object merging within the sections. */ 2928 2929static bfd_boolean 2930_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) 2931{ 2932 asection *sec; 2933 2934 if ((h->root.type == bfd_link_hash_defined 2935 || h->root.type == bfd_link_hash_defweak) 2936 && ((sec = h->root.u.def.section)->flags & SEC_MERGE) 2937 && sec->sec_info_type == SEC_INFO_TYPE_MERGE) 2938 { 2939 bfd *output_bfd = (bfd *) data; 2940 2941 h->root.u.def.value = 2942 _bfd_merged_section_offset (output_bfd, 2943 &h->root.u.def.section, 2944 elf_section_data (sec)->sec_info, 2945 h->root.u.def.value); 2946 } 2947 2948 return TRUE; 2949} 2950 2951/* Returns false if the symbol referred to by H should be considered 2952 to resolve local to the current module, and true if it should be 2953 considered to bind dynamically. */ 2954 2955bfd_boolean 2956_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, 2957 struct bfd_link_info *info, 2958 bfd_boolean not_local_protected) 2959{ 2960 bfd_boolean binding_stays_local_p; 2961 const struct elf_backend_data *bed; 2962 struct elf_link_hash_table *hash_table; 2963 2964 if (h == NULL) 2965 return FALSE; 2966 2967 while (h->root.type == bfd_link_hash_indirect 2968 || h->root.type == bfd_link_hash_warning) 2969 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2970 2971 /* If it was forced local, then clearly it's not dynamic. */ 2972 if (h->dynindx == -1) 2973 return FALSE; 2974 if (h->forced_local) 2975 return FALSE; 2976 2977 /* Identify the cases where name binding rules say that a 2978 visible symbol resolves locally. */ 2979 binding_stays_local_p = (bfd_link_executable (info) 2980 || SYMBOLIC_BIND (info, h)); 2981 2982 switch (ELF_ST_VISIBILITY (h->other)) 2983 { 2984 case STV_INTERNAL: 2985 case STV_HIDDEN: 2986 return FALSE; 2987 2988 case STV_PROTECTED: 2989 hash_table = elf_hash_table (info); 2990 if (!is_elf_hash_table (hash_table)) 2991 return FALSE; 2992 2993 bed = get_elf_backend_data (hash_table->dynobj); 2994 2995 /* Proper resolution for function pointer equality may require 2996 that these symbols perhaps be resolved dynamically, even though 2997 we should be resolving them to the current module. */ 2998 if (!not_local_protected || !bed->is_function_type (h->type)) 2999 binding_stays_local_p = TRUE; 3000 break; 3001 3002 default: 3003 break; 3004 } 3005 3006 /* If it isn't defined locally, then clearly it's dynamic. */ 3007 if (!h->def_regular && !ELF_COMMON_DEF_P (h)) 3008 return TRUE; 3009 3010 /* Otherwise, the symbol is dynamic if binding rules don't tell 3011 us that it remains local. */ 3012 return !binding_stays_local_p; 3013} 3014 3015/* Return true if the symbol referred to by H should be considered 3016 to resolve local to the current module, and false otherwise. Differs 3017 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of 3018 undefined symbols. The two functions are virtually identical except 3019 for the place where forced_local and dynindx == -1 are tested. If 3020 either of those tests are true, _bfd_elf_dynamic_symbol_p will say 3021 the symbol is local, while _bfd_elf_symbol_refs_local_p will say 3022 the symbol is local only for defined symbols. 3023 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as 3024 !_bfd_elf_symbol_refs_local_p, except that targets differ in their 3025 treatment of undefined weak symbols. For those that do not make 3026 undefined weak symbols dynamic, both functions may return false. */ 3027 3028bfd_boolean 3029_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, 3030 struct bfd_link_info *info, 3031 bfd_boolean local_protected) 3032{ 3033 const struct elf_backend_data *bed; 3034 struct elf_link_hash_table *hash_table; 3035 3036 /* If it's a local sym, of course we resolve locally. */ 3037 if (h == NULL) 3038 return TRUE; 3039 3040 /* STV_HIDDEN or STV_INTERNAL ones must be local. */ 3041 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN 3042 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) 3043 return TRUE; 3044 3045 /* Common symbols that become definitions don't get the DEF_REGULAR 3046 flag set, so test it first, and don't bail out. */ 3047 if (ELF_COMMON_DEF_P (h)) 3048 /* Do nothing. */; 3049 /* If we don't have a definition in a regular file, then we can't 3050 resolve locally. The sym is either undefined or dynamic. */ 3051 else if (!h->def_regular) 3052 return FALSE; 3053 3054 /* Forced local symbols resolve locally. */ 3055 if (h->forced_local) 3056 return TRUE; 3057 3058 /* As do non-dynamic symbols. */ 3059 if (h->dynindx == -1) 3060 return TRUE; 3061 3062 /* At this point, we know the symbol is defined and dynamic. In an 3063 executable it must resolve locally, likewise when building symbolic 3064 shared libraries. */ 3065 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) 3066 return TRUE; 3067 3068 /* Now deal with defined dynamic symbols in shared libraries. Ones 3069 with default visibility might not resolve locally. */ 3070 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) 3071 return FALSE; 3072 3073 hash_table = elf_hash_table (info); 3074 if (!is_elf_hash_table (hash_table)) 3075 return TRUE; 3076 3077 bed = get_elf_backend_data (hash_table->dynobj); 3078 3079 /* If extern_protected_data is false, STV_PROTECTED non-function 3080 symbols are local. */ 3081 if ((!info->extern_protected_data 3082 || (info->extern_protected_data < 0 3083 && !bed->extern_protected_data)) 3084 && !bed->is_function_type (h->type)) 3085 return TRUE; 3086 3087 /* Function pointer equality tests may require that STV_PROTECTED 3088 symbols be treated as dynamic symbols. If the address of a 3089 function not defined in an executable is set to that function's 3090 plt entry in the executable, then the address of the function in 3091 a shared library must also be the plt entry in the executable. */ 3092 return local_protected; 3093} 3094 3095/* Caches some TLS segment info, and ensures that the TLS segment vma is 3096 aligned. Returns the first TLS output section. */ 3097 3098struct bfd_section * 3099_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) 3100{ 3101 struct bfd_section *sec, *tls; 3102 unsigned int align = 0; 3103 3104 for (sec = obfd->sections; sec != NULL; sec = sec->next) 3105 if ((sec->flags & SEC_THREAD_LOCAL) != 0) 3106 break; 3107 tls = sec; 3108 3109 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) 3110 if (sec->alignment_power > align) 3111 align = sec->alignment_power; 3112 3113 elf_hash_table (info)->tls_sec = tls; 3114 3115 /* Ensure the alignment of the first section is the largest alignment, 3116 so that the tls segment starts aligned. */ 3117 if (tls != NULL) 3118 tls->alignment_power = align; 3119 3120 return tls; 3121} 3122 3123/* Return TRUE iff this is a non-common, definition of a non-function symbol. */ 3124static bfd_boolean 3125is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, 3126 Elf_Internal_Sym *sym) 3127{ 3128 const struct elf_backend_data *bed; 3129 3130 /* Local symbols do not count, but target specific ones might. */ 3131 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL 3132 && ELF_ST_BIND (sym->st_info) < STB_LOOS) 3133 return FALSE; 3134 3135 bed = get_elf_backend_data (abfd); 3136 /* Function symbols do not count. */ 3137 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) 3138 return FALSE; 3139 3140 /* If the section is undefined, then so is the symbol. */ 3141 if (sym->st_shndx == SHN_UNDEF) 3142 return FALSE; 3143 3144 /* If the symbol is defined in the common section, then 3145 it is a common definition and so does not count. */ 3146 if (bed->common_definition (sym)) 3147 return FALSE; 3148 3149 /* If the symbol is in a target specific section then we 3150 must rely upon the backend to tell us what it is. */ 3151 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) 3152 /* FIXME - this function is not coded yet: 3153 3154 return _bfd_is_global_symbol_definition (abfd, sym); 3155 3156 Instead for now assume that the definition is not global, 3157 Even if this is wrong, at least the linker will behave 3158 in the same way that it used to do. */ 3159 return FALSE; 3160 3161 return TRUE; 3162} 3163 3164/* Search the symbol table of the archive element of the archive ABFD 3165 whose archive map contains a mention of SYMDEF, and determine if 3166 the symbol is defined in this element. */ 3167static bfd_boolean 3168elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) 3169{ 3170 Elf_Internal_Shdr * hdr; 3171 size_t symcount; 3172 size_t extsymcount; 3173 size_t extsymoff; 3174 Elf_Internal_Sym *isymbuf; 3175 Elf_Internal_Sym *isym; 3176 Elf_Internal_Sym *isymend; 3177 bfd_boolean result; 3178 3179 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); 3180 if (abfd == NULL) 3181 return FALSE; 3182 3183 if (! bfd_check_format (abfd, bfd_object)) 3184 return FALSE; 3185 3186 /* Select the appropriate symbol table. If we don't know if the 3187 object file is an IR object, give linker LTO plugin a chance to 3188 get the correct symbol table. */ 3189 if (abfd->plugin_format == bfd_plugin_yes 3190#if BFD_SUPPORTS_PLUGINS 3191 || (abfd->plugin_format == bfd_plugin_unknown 3192 && bfd_link_plugin_object_p (abfd)) 3193#endif 3194 ) 3195 { 3196 /* Use the IR symbol table if the object has been claimed by 3197 plugin. */ 3198 abfd = abfd->plugin_dummy_bfd; 3199 hdr = &elf_tdata (abfd)->symtab_hdr; 3200 } 3201 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) 3202 hdr = &elf_tdata (abfd)->symtab_hdr; 3203 else 3204 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 3205 3206 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 3207 3208 /* The sh_info field of the symtab header tells us where the 3209 external symbols start. We don't care about the local symbols. */ 3210 if (elf_bad_symtab (abfd)) 3211 { 3212 extsymcount = symcount; 3213 extsymoff = 0; 3214 } 3215 else 3216 { 3217 extsymcount = symcount - hdr->sh_info; 3218 extsymoff = hdr->sh_info; 3219 } 3220 3221 if (extsymcount == 0) 3222 return FALSE; 3223 3224 /* Read in the symbol table. */ 3225 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, 3226 NULL, NULL, NULL); 3227 if (isymbuf == NULL) 3228 return FALSE; 3229 3230 /* Scan the symbol table looking for SYMDEF. */ 3231 result = FALSE; 3232 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) 3233 { 3234 const char *name; 3235 3236 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 3237 isym->st_name); 3238 if (name == NULL) 3239 break; 3240 3241 if (strcmp (name, symdef->name) == 0) 3242 { 3243 result = is_global_data_symbol_definition (abfd, isym); 3244 break; 3245 } 3246 } 3247 3248 free (isymbuf); 3249 3250 return result; 3251} 3252 3253/* Add an entry to the .dynamic table. */ 3254 3255bfd_boolean 3256_bfd_elf_add_dynamic_entry (struct bfd_link_info *info, 3257 bfd_vma tag, 3258 bfd_vma val) 3259{ 3260 struct elf_link_hash_table *hash_table; 3261 const struct elf_backend_data *bed; 3262 asection *s; 3263 bfd_size_type newsize; 3264 bfd_byte *newcontents; 3265 Elf_Internal_Dyn dyn; 3266 3267 hash_table = elf_hash_table (info); 3268 if (! is_elf_hash_table (hash_table)) 3269 return FALSE; 3270 3271 bed = get_elf_backend_data (hash_table->dynobj); 3272 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); 3273 BFD_ASSERT (s != NULL); 3274 3275 newsize = s->size + bed->s->sizeof_dyn; 3276 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); 3277 if (newcontents == NULL) 3278 return FALSE; 3279 3280 dyn.d_tag = tag; 3281 dyn.d_un.d_val = val; 3282 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); 3283 3284 s->size = newsize; 3285 s->contents = newcontents; 3286 3287 return TRUE; 3288} 3289 3290/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, 3291 otherwise just check whether one already exists. Returns -1 on error, 3292 1 if a DT_NEEDED tag already exists, and 0 on success. */ 3293 3294static int 3295elf_add_dt_needed_tag (bfd *abfd, 3296 struct bfd_link_info *info, 3297 const char *soname, 3298 bfd_boolean do_it) 3299{ 3300 struct elf_link_hash_table *hash_table; 3301 size_t strindex; 3302 3303 if (!_bfd_elf_link_create_dynstrtab (abfd, info)) 3304 return -1; 3305 3306 hash_table = elf_hash_table (info); 3307 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); 3308 if (strindex == (size_t) -1) 3309 return -1; 3310 3311 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) 3312 { 3313 asection *sdyn; 3314 const struct elf_backend_data *bed; 3315 bfd_byte *extdyn; 3316 3317 bed = get_elf_backend_data (hash_table->dynobj); 3318 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); 3319 if (sdyn != NULL) 3320 for (extdyn = sdyn->contents; 3321 extdyn < sdyn->contents + sdyn->size; 3322 extdyn += bed->s->sizeof_dyn) 3323 { 3324 Elf_Internal_Dyn dyn; 3325 3326 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); 3327 if (dyn.d_tag == DT_NEEDED 3328 && dyn.d_un.d_val == strindex) 3329 { 3330 _bfd_elf_strtab_delref (hash_table->dynstr, strindex); 3331 return 1; 3332 } 3333 } 3334 } 3335 3336 if (do_it) 3337 { 3338 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) 3339 return -1; 3340 3341 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) 3342 return -1; 3343 } 3344 else 3345 /* We were just checking for existence of the tag. */ 3346 _bfd_elf_strtab_delref (hash_table->dynstr, strindex); 3347 3348 return 0; 3349} 3350 3351/* Return true if SONAME is on the needed list between NEEDED and STOP 3352 (or the end of list if STOP is NULL), and needed by a library that 3353 will be loaded. */ 3354 3355static bfd_boolean 3356on_needed_list (const char *soname, 3357 struct bfd_link_needed_list *needed, 3358 struct bfd_link_needed_list *stop) 3359{ 3360 struct bfd_link_needed_list *look; 3361 for (look = needed; look != stop; look = look->next) 3362 if (strcmp (soname, look->name) == 0 3363 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 3364 /* If needed by a library that itself is not directly 3365 needed, recursively check whether that library is 3366 indirectly needed. Since we add DT_NEEDED entries to 3367 the end of the list, library dependencies appear after 3368 the library. Therefore search prior to the current 3369 LOOK, preventing possible infinite recursion. */ 3370 || on_needed_list (elf_dt_name (look->by), needed, look))) 3371 return TRUE; 3372 3373 return FALSE; 3374} 3375 3376/* Sort symbol by value, section, and size. */ 3377static int 3378elf_sort_symbol (const void *arg1, const void *arg2) 3379{ 3380 const struct elf_link_hash_entry *h1; 3381 const struct elf_link_hash_entry *h2; 3382 bfd_signed_vma vdiff; 3383 3384 h1 = *(const struct elf_link_hash_entry **) arg1; 3385 h2 = *(const struct elf_link_hash_entry **) arg2; 3386 vdiff = h1->root.u.def.value - h2->root.u.def.value; 3387 if (vdiff != 0) 3388 return vdiff > 0 ? 1 : -1; 3389 else 3390 { 3391 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; 3392 if (sdiff != 0) 3393 return sdiff > 0 ? 1 : -1; 3394 } 3395 vdiff = h1->size - h2->size; 3396 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; 3397} 3398 3399/* This function is used to adjust offsets into .dynstr for 3400 dynamic symbols. This is called via elf_link_hash_traverse. */ 3401 3402static bfd_boolean 3403elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) 3404{ 3405 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; 3406 3407 if (h->dynindx != -1) 3408 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); 3409 return TRUE; 3410} 3411 3412/* Assign string offsets in .dynstr, update all structures referencing 3413 them. */ 3414 3415static bfd_boolean 3416elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) 3417{ 3418 struct elf_link_hash_table *hash_table = elf_hash_table (info); 3419 struct elf_link_local_dynamic_entry *entry; 3420 struct elf_strtab_hash *dynstr = hash_table->dynstr; 3421 bfd *dynobj = hash_table->dynobj; 3422 asection *sdyn; 3423 bfd_size_type size; 3424 const struct elf_backend_data *bed; 3425 bfd_byte *extdyn; 3426 3427 _bfd_elf_strtab_finalize (dynstr); 3428 size = _bfd_elf_strtab_size (dynstr); 3429 3430 bed = get_elf_backend_data (dynobj); 3431 sdyn = bfd_get_linker_section (dynobj, ".dynamic"); 3432 BFD_ASSERT (sdyn != NULL); 3433 3434 /* Update all .dynamic entries referencing .dynstr strings. */ 3435 for (extdyn = sdyn->contents; 3436 extdyn < sdyn->contents + sdyn->size; 3437 extdyn += bed->s->sizeof_dyn) 3438 { 3439 Elf_Internal_Dyn dyn; 3440 3441 bed->s->swap_dyn_in (dynobj, extdyn, &dyn); 3442 switch (dyn.d_tag) 3443 { 3444 case DT_STRSZ: 3445 dyn.d_un.d_val = size; 3446 break; 3447 case DT_NEEDED: 3448 case DT_SONAME: 3449 case DT_RPATH: 3450 case DT_RUNPATH: 3451 case DT_FILTER: 3452 case DT_AUXILIARY: 3453 case DT_AUDIT: 3454 case DT_DEPAUDIT: 3455 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); 3456 break; 3457 default: 3458 continue; 3459 } 3460 bed->s->swap_dyn_out (dynobj, &dyn, extdyn); 3461 } 3462 3463 /* Now update local dynamic symbols. */ 3464 for (entry = hash_table->dynlocal; entry ; entry = entry->next) 3465 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, 3466 entry->isym.st_name); 3467 3468 /* And the rest of dynamic symbols. */ 3469 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); 3470 3471 /* Adjust version definitions. */ 3472 if (elf_tdata (output_bfd)->cverdefs) 3473 { 3474 asection *s; 3475 bfd_byte *p; 3476 size_t i; 3477 Elf_Internal_Verdef def; 3478 Elf_Internal_Verdaux defaux; 3479 3480 s = bfd_get_linker_section (dynobj, ".gnu.version_d"); 3481 p = s->contents; 3482 do 3483 { 3484 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, 3485 &def); 3486 p += sizeof (Elf_External_Verdef); 3487 if (def.vd_aux != sizeof (Elf_External_Verdef)) 3488 continue; 3489 for (i = 0; i < def.vd_cnt; ++i) 3490 { 3491 _bfd_elf_swap_verdaux_in (output_bfd, 3492 (Elf_External_Verdaux *) p, &defaux); 3493 defaux.vda_name = _bfd_elf_strtab_offset (dynstr, 3494 defaux.vda_name); 3495 _bfd_elf_swap_verdaux_out (output_bfd, 3496 &defaux, (Elf_External_Verdaux *) p); 3497 p += sizeof (Elf_External_Verdaux); 3498 } 3499 } 3500 while (def.vd_next); 3501 } 3502 3503 /* Adjust version references. */ 3504 if (elf_tdata (output_bfd)->verref) 3505 { 3506 asection *s; 3507 bfd_byte *p; 3508 size_t i; 3509 Elf_Internal_Verneed need; 3510 Elf_Internal_Vernaux needaux; 3511 3512 s = bfd_get_linker_section (dynobj, ".gnu.version_r"); 3513 p = s->contents; 3514 do 3515 { 3516 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, 3517 &need); 3518 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); 3519 _bfd_elf_swap_verneed_out (output_bfd, &need, 3520 (Elf_External_Verneed *) p); 3521 p += sizeof (Elf_External_Verneed); 3522 for (i = 0; i < need.vn_cnt; ++i) 3523 { 3524 _bfd_elf_swap_vernaux_in (output_bfd, 3525 (Elf_External_Vernaux *) p, &needaux); 3526 needaux.vna_name = _bfd_elf_strtab_offset (dynstr, 3527 needaux.vna_name); 3528 _bfd_elf_swap_vernaux_out (output_bfd, 3529 &needaux, 3530 (Elf_External_Vernaux *) p); 3531 p += sizeof (Elf_External_Vernaux); 3532 } 3533 } 3534 while (need.vn_next); 3535 } 3536 3537 return TRUE; 3538} 3539 3540/* Return TRUE iff relocations for INPUT are compatible with OUTPUT. 3541 The default is to only match when the INPUT and OUTPUT are exactly 3542 the same target. */ 3543 3544bfd_boolean 3545_bfd_elf_default_relocs_compatible (const bfd_target *input, 3546 const bfd_target *output) 3547{ 3548 return input == output; 3549} 3550 3551/* Return TRUE iff relocations for INPUT are compatible with OUTPUT. 3552 This version is used when different targets for the same architecture 3553 are virtually identical. */ 3554 3555bfd_boolean 3556_bfd_elf_relocs_compatible (const bfd_target *input, 3557 const bfd_target *output) 3558{ 3559 const struct elf_backend_data *obed, *ibed; 3560 3561 if (input == output) 3562 return TRUE; 3563 3564 ibed = xvec_get_elf_backend_data (input); 3565 obed = xvec_get_elf_backend_data (output); 3566 3567 if (ibed->arch != obed->arch) 3568 return FALSE; 3569 3570 /* If both backends are using this function, deem them compatible. */ 3571 return ibed->relocs_compatible == obed->relocs_compatible; 3572} 3573 3574/* Make a special call to the linker "notice" function to tell it that 3575 we are about to handle an as-needed lib, or have finished 3576 processing the lib. */ 3577 3578bfd_boolean 3579_bfd_elf_notice_as_needed (bfd *ibfd, 3580 struct bfd_link_info *info, 3581 enum notice_asneeded_action act) 3582{ 3583 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); 3584} 3585 3586/* Check relocations an ELF object file. */ 3587 3588bfd_boolean 3589_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) 3590{ 3591 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3592 struct elf_link_hash_table *htab = elf_hash_table (info); 3593 3594 /* If this object is the same format as the output object, and it is 3595 not a shared library, then let the backend look through the 3596 relocs. 3597 3598 This is required to build global offset table entries and to 3599 arrange for dynamic relocs. It is not required for the 3600 particular common case of linking non PIC code, even when linking 3601 against shared libraries, but unfortunately there is no way of 3602 knowing whether an object file has been compiled PIC or not. 3603 Looking through the relocs is not particularly time consuming. 3604 The problem is that we must either (1) keep the relocs in memory, 3605 which causes the linker to require additional runtime memory or 3606 (2) read the relocs twice from the input file, which wastes time. 3607 This would be a good case for using mmap. 3608 3609 I have no idea how to handle linking PIC code into a file of a 3610 different format. It probably can't be done. */ 3611 if ((abfd->flags & DYNAMIC) == 0 3612 && is_elf_hash_table (htab) 3613 && bed->check_relocs != NULL 3614 && elf_object_id (abfd) == elf_hash_table_id (htab) 3615 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) 3616 { 3617 asection *o; 3618 3619 for (o = abfd->sections; o != NULL; o = o->next) 3620 { 3621 Elf_Internal_Rela *internal_relocs; 3622 bfd_boolean ok; 3623 3624 /* Don't check relocations in excluded sections. */ 3625 if ((o->flags & SEC_RELOC) == 0 3626 || (o->flags & SEC_EXCLUDE) != 0 3627 || o->reloc_count == 0 3628 || ((info->strip == strip_all || info->strip == strip_debugger) 3629 && (o->flags & SEC_DEBUGGING) != 0) 3630 || bfd_is_abs_section (o->output_section)) 3631 continue; 3632 3633 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, 3634 info->keep_memory); 3635 if (internal_relocs == NULL) 3636 return FALSE; 3637 3638 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); 3639 3640 if (elf_section_data (o)->relocs != internal_relocs) 3641 free (internal_relocs); 3642 3643 if (! ok) 3644 return FALSE; 3645 } 3646 } 3647 3648 return TRUE; 3649} 3650 3651/* Add symbols from an ELF object file to the linker hash table. */ 3652 3653static bfd_boolean 3654elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) 3655{ 3656 Elf_Internal_Ehdr *ehdr; 3657 Elf_Internal_Shdr *hdr; 3658 size_t symcount; 3659 size_t extsymcount; 3660 size_t extsymoff; 3661 struct elf_link_hash_entry **sym_hash; 3662 bfd_boolean dynamic; 3663 Elf_External_Versym *extversym = NULL; 3664 Elf_External_Versym *ever; 3665 struct elf_link_hash_entry *weaks; 3666 struct elf_link_hash_entry **nondeflt_vers = NULL; 3667 size_t nondeflt_vers_cnt = 0; 3668 Elf_Internal_Sym *isymbuf = NULL; 3669 Elf_Internal_Sym *isym; 3670 Elf_Internal_Sym *isymend; 3671 const struct elf_backend_data *bed; 3672 bfd_boolean add_needed; 3673 struct elf_link_hash_table *htab; 3674 bfd_size_type amt; 3675 void *alloc_mark = NULL; 3676 struct bfd_hash_entry **old_table = NULL; 3677 unsigned int old_size = 0; 3678 unsigned int old_count = 0; 3679 void *old_tab = NULL; 3680 void *old_ent; 3681 struct bfd_link_hash_entry *old_undefs = NULL; 3682 struct bfd_link_hash_entry *old_undefs_tail = NULL; 3683 void *old_strtab = NULL; 3684 size_t tabsize = 0; 3685 asection *s; 3686 bfd_boolean just_syms; 3687 3688 htab = elf_hash_table (info); 3689 bed = get_elf_backend_data (abfd); 3690 3691 if ((abfd->flags & DYNAMIC) == 0) 3692 dynamic = FALSE; 3693 else 3694 { 3695 dynamic = TRUE; 3696 3697 /* You can't use -r against a dynamic object. Also, there's no 3698 hope of using a dynamic object which does not exactly match 3699 the format of the output file. */ 3700 if (bfd_link_relocatable (info) 3701 || !is_elf_hash_table (htab) 3702 || info->output_bfd->xvec != abfd->xvec) 3703 { 3704 if (bfd_link_relocatable (info)) 3705 bfd_set_error (bfd_error_invalid_operation); 3706 else 3707 bfd_set_error (bfd_error_wrong_format); 3708 goto error_return; 3709 } 3710 } 3711 3712 ehdr = elf_elfheader (abfd); 3713 if (info->warn_alternate_em 3714 && bed->elf_machine_code != ehdr->e_machine 3715 && ((bed->elf_machine_alt1 != 0 3716 && ehdr->e_machine == bed->elf_machine_alt1) 3717 || (bed->elf_machine_alt2 != 0 3718 && ehdr->e_machine == bed->elf_machine_alt2))) 3719 info->callbacks->einfo 3720 /* xgettext:c-format */ 3721 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"), 3722 ehdr->e_machine, abfd, bed->elf_machine_code); 3723 3724 /* As a GNU extension, any input sections which are named 3725 .gnu.warning.SYMBOL are treated as warning symbols for the given 3726 symbol. This differs from .gnu.warning sections, which generate 3727 warnings when they are included in an output file. */ 3728 /* PR 12761: Also generate this warning when building shared libraries. */ 3729 for (s = abfd->sections; s != NULL; s = s->next) 3730 { 3731 const char *name; 3732 3733 name = bfd_get_section_name (abfd, s); 3734 if (CONST_STRNEQ (name, ".gnu.warning.")) 3735 { 3736 char *msg; 3737 bfd_size_type sz; 3738 3739 name += sizeof ".gnu.warning." - 1; 3740 3741 /* If this is a shared object, then look up the symbol 3742 in the hash table. If it is there, and it is already 3743 been defined, then we will not be using the entry 3744 from this shared object, so we don't need to warn. 3745 FIXME: If we see the definition in a regular object 3746 later on, we will warn, but we shouldn't. The only 3747 fix is to keep track of what warnings we are supposed 3748 to emit, and then handle them all at the end of the 3749 link. */ 3750 if (dynamic) 3751 { 3752 struct elf_link_hash_entry *h; 3753 3754 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); 3755 3756 /* FIXME: What about bfd_link_hash_common? */ 3757 if (h != NULL 3758 && (h->root.type == bfd_link_hash_defined 3759 || h->root.type == bfd_link_hash_defweak)) 3760 continue; 3761 } 3762 3763 sz = s->size; 3764 msg = (char *) bfd_alloc (abfd, sz + 1); 3765 if (msg == NULL) 3766 goto error_return; 3767 3768 if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) 3769 goto error_return; 3770 3771 msg[sz] = '\0'; 3772 3773 if (! (_bfd_generic_link_add_one_symbol 3774 (info, abfd, name, BSF_WARNING, s, 0, msg, 3775 FALSE, bed->collect, NULL))) 3776 goto error_return; 3777 3778 if (bfd_link_executable (info)) 3779 { 3780 /* Clobber the section size so that the warning does 3781 not get copied into the output file. */ 3782 s->size = 0; 3783 3784 /* Also set SEC_EXCLUDE, so that symbols defined in 3785 the warning section don't get copied to the output. */ 3786 s->flags |= SEC_EXCLUDE; 3787 } 3788 } 3789 } 3790 3791 just_syms = ((s = abfd->sections) != NULL 3792 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); 3793 3794 add_needed = TRUE; 3795 if (! dynamic) 3796 { 3797 /* If we are creating a shared library, create all the dynamic 3798 sections immediately. We need to attach them to something, 3799 so we attach them to this BFD, provided it is the right 3800 format and is not from ld --just-symbols. Always create the 3801 dynamic sections for -E/--dynamic-list. FIXME: If there 3802 are no input BFD's of the same format as the output, we can't 3803 make a shared library. */ 3804 if (!just_syms 3805 && (bfd_link_pic (info) 3806 || (!bfd_link_relocatable (info) 3807 && (info->export_dynamic || info->dynamic))) 3808 && is_elf_hash_table (htab) 3809 && info->output_bfd->xvec == abfd->xvec 3810 && !htab->dynamic_sections_created) 3811 { 3812 if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) 3813 goto error_return; 3814 } 3815 } 3816 else if (!is_elf_hash_table (htab)) 3817 goto error_return; 3818 else 3819 { 3820 const char *soname = NULL; 3821 char *audit = NULL; 3822 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; 3823 const Elf_Internal_Phdr *phdr; 3824 int ret; 3825 3826 /* ld --just-symbols and dynamic objects don't mix very well. 3827 ld shouldn't allow it. */ 3828 if (just_syms) 3829 abort (); 3830 3831 /* If this dynamic lib was specified on the command line with 3832 --as-needed in effect, then we don't want to add a DT_NEEDED 3833 tag unless the lib is actually used. Similary for libs brought 3834 in by another lib's DT_NEEDED. When --no-add-needed is used 3835 on a dynamic lib, we don't want to add a DT_NEEDED entry for 3836 any dynamic library in DT_NEEDED tags in the dynamic lib at 3837 all. */ 3838 add_needed = (elf_dyn_lib_class (abfd) 3839 & (DYN_AS_NEEDED | DYN_DT_NEEDED 3840 | DYN_NO_NEEDED)) == 0; 3841 3842 s = bfd_get_section_by_name (abfd, ".dynamic"); 3843 if (s != NULL) 3844 { 3845 bfd_byte *dynbuf; 3846 bfd_byte *extdyn; 3847 unsigned int elfsec; 3848 unsigned long shlink; 3849 3850 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) 3851 { 3852error_free_dyn: 3853 free (dynbuf); 3854 goto error_return; 3855 } 3856 3857 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 3858 if (elfsec == SHN_BAD) 3859 goto error_free_dyn; 3860 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 3861 3862 for (extdyn = dynbuf; 3863 extdyn < dynbuf + s->size; 3864 extdyn += bed->s->sizeof_dyn) 3865 { 3866 Elf_Internal_Dyn dyn; 3867 3868 bed->s->swap_dyn_in (abfd, extdyn, &dyn); 3869 if (dyn.d_tag == DT_SONAME) 3870 { 3871 unsigned int tagv = dyn.d_un.d_val; 3872 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3873 if (soname == NULL) 3874 goto error_free_dyn; 3875 } 3876 if (dyn.d_tag == DT_NEEDED) 3877 { 3878 struct bfd_link_needed_list *n, **pn; 3879 char *fnm, *anm; 3880 unsigned int tagv = dyn.d_un.d_val; 3881 3882 amt = sizeof (struct bfd_link_needed_list); 3883 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 3884 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3885 if (n == NULL || fnm == NULL) 3886 goto error_free_dyn; 3887 amt = strlen (fnm) + 1; 3888 anm = (char *) bfd_alloc (abfd, amt); 3889 if (anm == NULL) 3890 goto error_free_dyn; 3891 memcpy (anm, fnm, amt); 3892 n->name = anm; 3893 n->by = abfd; 3894 n->next = NULL; 3895 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) 3896 ; 3897 *pn = n; 3898 } 3899 if (dyn.d_tag == DT_RUNPATH) 3900 { 3901 struct bfd_link_needed_list *n, **pn; 3902 char *fnm, *anm; 3903 unsigned int tagv = dyn.d_un.d_val; 3904 3905 amt = sizeof (struct bfd_link_needed_list); 3906 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 3907 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3908 if (n == NULL || fnm == NULL) 3909 goto error_free_dyn; 3910 amt = strlen (fnm) + 1; 3911 anm = (char *) bfd_alloc (abfd, amt); 3912 if (anm == NULL) 3913 goto error_free_dyn; 3914 memcpy (anm, fnm, amt); 3915 n->name = anm; 3916 n->by = abfd; 3917 n->next = NULL; 3918 for (pn = & runpath; 3919 *pn != NULL; 3920 pn = &(*pn)->next) 3921 ; 3922 *pn = n; 3923 } 3924 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ 3925 if (!runpath && dyn.d_tag == DT_RPATH) 3926 { 3927 struct bfd_link_needed_list *n, **pn; 3928 char *fnm, *anm; 3929 unsigned int tagv = dyn.d_un.d_val; 3930 3931 amt = sizeof (struct bfd_link_needed_list); 3932 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 3933 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3934 if (n == NULL || fnm == NULL) 3935 goto error_free_dyn; 3936 amt = strlen (fnm) + 1; 3937 anm = (char *) bfd_alloc (abfd, amt); 3938 if (anm == NULL) 3939 goto error_free_dyn; 3940 memcpy (anm, fnm, amt); 3941 n->name = anm; 3942 n->by = abfd; 3943 n->next = NULL; 3944 for (pn = & rpath; 3945 *pn != NULL; 3946 pn = &(*pn)->next) 3947 ; 3948 *pn = n; 3949 } 3950 if (dyn.d_tag == DT_AUDIT) 3951 { 3952 unsigned int tagv = dyn.d_un.d_val; 3953 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3954 } 3955 } 3956 3957 free (dynbuf); 3958 } 3959 3960 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that 3961 frees all more recently bfd_alloc'd blocks as well. */ 3962 if (runpath) 3963 rpath = runpath; 3964 3965 if (rpath) 3966 { 3967 struct bfd_link_needed_list **pn; 3968 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) 3969 ; 3970 *pn = rpath; 3971 } 3972 3973 /* If we have a PT_GNU_RELRO program header, mark as read-only 3974 all sections contained fully therein. This makes relro 3975 shared library sections appear as they will at run-time. */ 3976 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum; 3977 while (--phdr >= elf_tdata (abfd)->phdr) 3978 if (phdr->p_type == PT_GNU_RELRO) 3979 { 3980 for (s = abfd->sections; s != NULL; s = s->next) 3981 if ((s->flags & SEC_ALLOC) != 0 3982 && s->vma >= phdr->p_vaddr 3983 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz) 3984 s->flags |= SEC_READONLY; 3985 break; 3986 } 3987 3988 /* We do not want to include any of the sections in a dynamic 3989 object in the output file. We hack by simply clobbering the 3990 list of sections in the BFD. This could be handled more 3991 cleanly by, say, a new section flag; the existing 3992 SEC_NEVER_LOAD flag is not the one we want, because that one 3993 still implies that the section takes up space in the output 3994 file. */ 3995 bfd_section_list_clear (abfd); 3996 3997 /* Find the name to use in a DT_NEEDED entry that refers to this 3998 object. If the object has a DT_SONAME entry, we use it. 3999 Otherwise, if the generic linker stuck something in 4000 elf_dt_name, we use that. Otherwise, we just use the file 4001 name. */ 4002 if (soname == NULL || *soname == '\0') 4003 { 4004 soname = elf_dt_name (abfd); 4005 if (soname == NULL || *soname == '\0') 4006 soname = bfd_get_filename (abfd); 4007 } 4008 4009 /* Save the SONAME because sometimes the linker emulation code 4010 will need to know it. */ 4011 elf_dt_name (abfd) = soname; 4012 4013 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); 4014 if (ret < 0) 4015 goto error_return; 4016 4017 /* If we have already included this dynamic object in the 4018 link, just ignore it. There is no reason to include a 4019 particular dynamic object more than once. */ 4020 if (ret > 0) 4021 return TRUE; 4022 4023 /* Save the DT_AUDIT entry for the linker emulation code. */ 4024 elf_dt_audit (abfd) = audit; 4025 } 4026 4027 /* If this is a dynamic object, we always link against the .dynsym 4028 symbol table, not the .symtab symbol table. The dynamic linker 4029 will only see the .dynsym symbol table, so there is no reason to 4030 look at .symtab for a dynamic object. */ 4031 4032 if (! dynamic || elf_dynsymtab (abfd) == 0) 4033 hdr = &elf_tdata (abfd)->symtab_hdr; 4034 else 4035 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 4036 4037 symcount = hdr->sh_size / bed->s->sizeof_sym; 4038 4039 /* The sh_info field of the symtab header tells us where the 4040 external symbols start. We don't care about the local symbols at 4041 this point. */ 4042 if (elf_bad_symtab (abfd)) 4043 { 4044 extsymcount = symcount; 4045 extsymoff = 0; 4046 } 4047 else 4048 { 4049 extsymcount = symcount - hdr->sh_info; 4050 extsymoff = hdr->sh_info; 4051 } 4052 4053 sym_hash = elf_sym_hashes (abfd); 4054 if (extsymcount != 0) 4055 { 4056 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, 4057 NULL, NULL, NULL); 4058 if (isymbuf == NULL) 4059 goto error_return; 4060 4061 if (sym_hash == NULL) 4062 { 4063 /* We store a pointer to the hash table entry for each 4064 external symbol. */ 4065 amt = extsymcount; 4066 amt *= sizeof (struct elf_link_hash_entry *); 4067 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); 4068 if (sym_hash == NULL) 4069 goto error_free_sym; 4070 elf_sym_hashes (abfd) = sym_hash; 4071 } 4072 } 4073 4074 if (dynamic) 4075 { 4076 /* Read in any version definitions. */ 4077 if (!_bfd_elf_slurp_version_tables (abfd, 4078 info->default_imported_symver)) 4079 goto error_free_sym; 4080 4081 /* Read in the symbol versions, but don't bother to convert them 4082 to internal format. */ 4083 if (elf_dynversym (abfd) != 0) 4084 { 4085 Elf_Internal_Shdr *versymhdr; 4086 4087 versymhdr = &elf_tdata (abfd)->dynversym_hdr; 4088 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); 4089 if (extversym == NULL) 4090 goto error_free_sym; 4091 amt = versymhdr->sh_size; 4092 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 4093 || bfd_bread (extversym, amt, abfd) != amt) 4094 goto error_free_vers; 4095 } 4096 } 4097 4098 /* If we are loading an as-needed shared lib, save the symbol table 4099 state before we start adding symbols. If the lib turns out 4100 to be unneeded, restore the state. */ 4101 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) 4102 { 4103 unsigned int i; 4104 size_t entsize; 4105 4106 for (entsize = 0, i = 0; i < htab->root.table.size; i++) 4107 { 4108 struct bfd_hash_entry *p; 4109 struct elf_link_hash_entry *h; 4110 4111 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 4112 { 4113 h = (struct elf_link_hash_entry *) p; 4114 entsize += htab->root.table.entsize; 4115 if (h->root.type == bfd_link_hash_warning) 4116 entsize += htab->root.table.entsize; 4117 } 4118 } 4119 4120 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); 4121 old_tab = bfd_malloc (tabsize + entsize); 4122 if (old_tab == NULL) 4123 goto error_free_vers; 4124 4125 /* Remember the current objalloc pointer, so that all mem for 4126 symbols added can later be reclaimed. */ 4127 alloc_mark = bfd_hash_allocate (&htab->root.table, 1); 4128 if (alloc_mark == NULL) 4129 goto error_free_vers; 4130 4131 /* Make a special call to the linker "notice" function to 4132 tell it that we are about to handle an as-needed lib. */ 4133 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) 4134 goto error_free_vers; 4135 4136 /* Clone the symbol table. Remember some pointers into the 4137 symbol table, and dynamic symbol count. */ 4138 old_ent = (char *) old_tab + tabsize; 4139 memcpy (old_tab, htab->root.table.table, tabsize); 4140 old_undefs = htab->root.undefs; 4141 old_undefs_tail = htab->root.undefs_tail; 4142 old_table = htab->root.table.table; 4143 old_size = htab->root.table.size; 4144 old_count = htab->root.table.count; 4145 old_strtab = _bfd_elf_strtab_save (htab->dynstr); 4146 if (old_strtab == NULL) 4147 goto error_free_vers; 4148 4149 for (i = 0; i < htab->root.table.size; i++) 4150 { 4151 struct bfd_hash_entry *p; 4152 struct elf_link_hash_entry *h; 4153 4154 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 4155 { 4156 memcpy (old_ent, p, htab->root.table.entsize); 4157 old_ent = (char *) old_ent + htab->root.table.entsize; 4158 h = (struct elf_link_hash_entry *) p; 4159 if (h->root.type == bfd_link_hash_warning) 4160 { 4161 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); 4162 old_ent = (char *) old_ent + htab->root.table.entsize; 4163 } 4164 } 4165 } 4166 } 4167 4168 weaks = NULL; 4169 ever = extversym != NULL ? extversym + extsymoff : NULL; 4170 for (isym = isymbuf, isymend = isymbuf + extsymcount; 4171 isym < isymend; 4172 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) 4173 { 4174 int bind; 4175 bfd_vma value; 4176 asection *sec, *new_sec; 4177 flagword flags; 4178 const char *name; 4179 struct elf_link_hash_entry *h; 4180 struct elf_link_hash_entry *hi; 4181 bfd_boolean definition; 4182 bfd_boolean size_change_ok; 4183 bfd_boolean type_change_ok; 4184 bfd_boolean new_weakdef; 4185 bfd_boolean new_weak; 4186 bfd_boolean old_weak; 4187 bfd_boolean override; 4188 bfd_boolean common; 4189 bfd_boolean discarded; 4190 unsigned int old_alignment; 4191 bfd *old_bfd; 4192 bfd_boolean matched; 4193 4194 override = FALSE; 4195 4196 flags = BSF_NO_FLAGS; 4197 sec = NULL; 4198 value = isym->st_value; 4199 common = bed->common_definition (isym); 4200 discarded = FALSE; 4201 4202 bind = ELF_ST_BIND (isym->st_info); 4203 switch (bind) 4204 { 4205 case STB_LOCAL: 4206 /* This should be impossible, since ELF requires that all 4207 global symbols follow all local symbols, and that sh_info 4208 point to the first global symbol. Unfortunately, Irix 5 4209 screws this up. */ 4210 continue; 4211 4212 case STB_GLOBAL: 4213 if (isym->st_shndx != SHN_UNDEF && !common) 4214 flags = BSF_GLOBAL; 4215 break; 4216 4217 case STB_WEAK: 4218 flags = BSF_WEAK; 4219 break; 4220 4221 case STB_GNU_UNIQUE: 4222 flags = BSF_GNU_UNIQUE; 4223 break; 4224 4225 default: 4226 /* Leave it up to the processor backend. */ 4227 break; 4228 } 4229 4230 if (isym->st_shndx == SHN_UNDEF) 4231 sec = bfd_und_section_ptr; 4232 else if (isym->st_shndx == SHN_ABS) 4233 sec = bfd_abs_section_ptr; 4234 else if (isym->st_shndx == SHN_COMMON) 4235 { 4236 sec = bfd_com_section_ptr; 4237 /* What ELF calls the size we call the value. What ELF 4238 calls the value we call the alignment. */ 4239 value = isym->st_size; 4240 } 4241 else 4242 { 4243 sec = bfd_section_from_elf_index (abfd, isym->st_shndx); 4244 if (sec == NULL) 4245 sec = bfd_abs_section_ptr; 4246 else if (discarded_section (sec)) 4247 { 4248 /* Symbols from discarded section are undefined. We keep 4249 its visibility. */ 4250 sec = bfd_und_section_ptr; 4251 discarded = TRUE; 4252 isym->st_shndx = SHN_UNDEF; 4253 } 4254 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 4255 value -= sec->vma; 4256 } 4257 4258 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 4259 isym->st_name); 4260 if (name == NULL) 4261 goto error_free_vers; 4262 4263 if (isym->st_shndx == SHN_COMMON 4264 && (abfd->flags & BFD_PLUGIN) != 0) 4265 { 4266 asection *xc = bfd_get_section_by_name (abfd, "COMMON"); 4267 4268 if (xc == NULL) 4269 { 4270 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP 4271 | SEC_EXCLUDE); 4272 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); 4273 if (xc == NULL) 4274 goto error_free_vers; 4275 } 4276 sec = xc; 4277 } 4278 else if (isym->st_shndx == SHN_COMMON 4279 && ELF_ST_TYPE (isym->st_info) == STT_TLS 4280 && !bfd_link_relocatable (info)) 4281 { 4282 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); 4283 4284 if (tcomm == NULL) 4285 { 4286 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON 4287 | SEC_LINKER_CREATED); 4288 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); 4289 if (tcomm == NULL) 4290 goto error_free_vers; 4291 } 4292 sec = tcomm; 4293 } 4294 else if (bed->elf_add_symbol_hook) 4295 { 4296 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, 4297 &sec, &value)) 4298 goto error_free_vers; 4299 4300 /* The hook function sets the name to NULL if this symbol 4301 should be skipped for some reason. */ 4302 if (name == NULL) 4303 continue; 4304 } 4305 4306 /* Sanity check that all possibilities were handled. */ 4307 if (sec == NULL) 4308 { 4309 bfd_set_error (bfd_error_bad_value); 4310 goto error_free_vers; 4311 } 4312 4313 /* Silently discard TLS symbols from --just-syms. There's 4314 no way to combine a static TLS block with a new TLS block 4315 for this executable. */ 4316 if (ELF_ST_TYPE (isym->st_info) == STT_TLS 4317 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 4318 continue; 4319 4320 if (bfd_is_und_section (sec) 4321 || bfd_is_com_section (sec)) 4322 definition = FALSE; 4323 else 4324 definition = TRUE; 4325 4326 size_change_ok = FALSE; 4327 type_change_ok = bed->type_change_ok; 4328 old_weak = FALSE; 4329 matched = FALSE; 4330 old_alignment = 0; 4331 old_bfd = NULL; 4332 new_sec = sec; 4333 4334 if (is_elf_hash_table (htab)) 4335 { 4336 Elf_Internal_Versym iver; 4337 unsigned int vernum = 0; 4338 bfd_boolean skip; 4339 4340 if (ever == NULL) 4341 { 4342 if (info->default_imported_symver) 4343 /* Use the default symbol version created earlier. */ 4344 iver.vs_vers = elf_tdata (abfd)->cverdefs; 4345 else 4346 iver.vs_vers = 0; 4347 } 4348 else 4349 _bfd_elf_swap_versym_in (abfd, ever, &iver); 4350 4351 vernum = iver.vs_vers & VERSYM_VERSION; 4352 4353 /* If this is a hidden symbol, or if it is not version 4354 1, we append the version name to the symbol name. 4355 However, we do not modify a non-hidden absolute symbol 4356 if it is not a function, because it might be the version 4357 symbol itself. FIXME: What if it isn't? */ 4358 if ((iver.vs_vers & VERSYM_HIDDEN) != 0 4359 || (vernum > 1 4360 && (!bfd_is_abs_section (sec) 4361 || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) 4362 { 4363 const char *verstr; 4364 size_t namelen, verlen, newlen; 4365 char *newname, *p; 4366 4367 if (isym->st_shndx != SHN_UNDEF) 4368 { 4369 if (vernum > elf_tdata (abfd)->cverdefs) 4370 verstr = NULL; 4371 else if (vernum > 1) 4372 verstr = 4373 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 4374 else 4375 verstr = ""; 4376 4377 if (verstr == NULL) 4378 { 4379 _bfd_error_handler 4380 /* xgettext:c-format */ 4381 (_("%B: %s: invalid version %u (max %d)"), 4382 abfd, name, vernum, 4383 elf_tdata (abfd)->cverdefs); 4384 bfd_set_error (bfd_error_bad_value); 4385 goto error_free_vers; 4386 } 4387 } 4388 else 4389 { 4390 /* We cannot simply test for the number of 4391 entries in the VERNEED section since the 4392 numbers for the needed versions do not start 4393 at 0. */ 4394 Elf_Internal_Verneed *t; 4395 4396 verstr = NULL; 4397 for (t = elf_tdata (abfd)->verref; 4398 t != NULL; 4399 t = t->vn_nextref) 4400 { 4401 Elf_Internal_Vernaux *a; 4402 4403 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 4404 { 4405 if (a->vna_other == vernum) 4406 { 4407 verstr = a->vna_nodename; 4408 break; 4409 } 4410 } 4411 if (a != NULL) 4412 break; 4413 } 4414 if (verstr == NULL) 4415 { 4416 _bfd_error_handler 4417 /* xgettext:c-format */ 4418 (_("%B: %s: invalid needed version %d"), 4419 abfd, name, vernum); 4420 bfd_set_error (bfd_error_bad_value); 4421 goto error_free_vers; 4422 } 4423 } 4424 4425 namelen = strlen (name); 4426 verlen = strlen (verstr); 4427 newlen = namelen + verlen + 2; 4428 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 4429 && isym->st_shndx != SHN_UNDEF) 4430 ++newlen; 4431 4432 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); 4433 if (newname == NULL) 4434 goto error_free_vers; 4435 memcpy (newname, name, namelen); 4436 p = newname + namelen; 4437 *p++ = ELF_VER_CHR; 4438 /* If this is a defined non-hidden version symbol, 4439 we add another @ to the name. This indicates the 4440 default version of the symbol. */ 4441 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 4442 && isym->st_shndx != SHN_UNDEF) 4443 *p++ = ELF_VER_CHR; 4444 memcpy (p, verstr, verlen + 1); 4445 4446 name = newname; 4447 } 4448 4449 /* If this symbol has default visibility and the user has 4450 requested we not re-export it, then mark it as hidden. */ 4451 if (!bfd_is_und_section (sec) 4452 && !dynamic 4453 && abfd->no_export 4454 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) 4455 isym->st_other = (STV_HIDDEN 4456 | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); 4457 4458 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, 4459 sym_hash, &old_bfd, &old_weak, 4460 &old_alignment, &skip, &override, 4461 &type_change_ok, &size_change_ok, 4462 &matched)) 4463 goto error_free_vers; 4464 4465 if (skip) 4466 continue; 4467 4468 /* Override a definition only if the new symbol matches the 4469 existing one. */ 4470 if (override && matched) 4471 definition = FALSE; 4472 4473 h = *sym_hash; 4474 while (h->root.type == bfd_link_hash_indirect 4475 || h->root.type == bfd_link_hash_warning) 4476 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4477 4478 if (elf_tdata (abfd)->verdef != NULL 4479 && vernum > 1 4480 && definition) 4481 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; 4482 } 4483 4484 if (! (_bfd_generic_link_add_one_symbol 4485 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, 4486 (struct bfd_link_hash_entry **) sym_hash))) 4487 goto error_free_vers; 4488 4489 if ((flags & BSF_GNU_UNIQUE) 4490 && (abfd->flags & DYNAMIC) == 0 4491 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour) 4492 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique; 4493 4494 h = *sym_hash; 4495 /* We need to make sure that indirect symbol dynamic flags are 4496 updated. */ 4497 hi = h; 4498 while (h->root.type == bfd_link_hash_indirect 4499 || h->root.type == bfd_link_hash_warning) 4500 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4501 4502 /* Setting the index to -3 tells elf_link_output_extsym that 4503 this symbol is defined in a discarded section. */ 4504 if (discarded) 4505 h->indx = -3; 4506 4507 *sym_hash = h; 4508 4509 new_weak = (flags & BSF_WEAK) != 0; 4510 new_weakdef = FALSE; 4511 if (dynamic 4512 && definition 4513 && new_weak 4514 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) 4515 && is_elf_hash_table (htab) 4516 && h->u.weakdef == NULL) 4517 { 4518 /* Keep a list of all weak defined non function symbols from 4519 a dynamic object, using the weakdef field. Later in this 4520 function we will set the weakdef field to the correct 4521 value. We only put non-function symbols from dynamic 4522 objects on this list, because that happens to be the only 4523 time we need to know the normal symbol corresponding to a 4524 weak symbol, and the information is time consuming to 4525 figure out. If the weakdef field is not already NULL, 4526 then this symbol was already defined by some previous 4527 dynamic object, and we will be using that previous 4528 definition anyhow. */ 4529 4530 h->u.weakdef = weaks; 4531 weaks = h; 4532 new_weakdef = TRUE; 4533 } 4534 4535 /* Set the alignment of a common symbol. */ 4536 if ((common || bfd_is_com_section (sec)) 4537 && h->root.type == bfd_link_hash_common) 4538 { 4539 unsigned int align; 4540 4541 if (common) 4542 align = bfd_log2 (isym->st_value); 4543 else 4544 { 4545 /* The new symbol is a common symbol in a shared object. 4546 We need to get the alignment from the section. */ 4547 align = new_sec->alignment_power; 4548 } 4549 if (align > old_alignment) 4550 h->root.u.c.p->alignment_power = align; 4551 else 4552 h->root.u.c.p->alignment_power = old_alignment; 4553 } 4554 4555 if (is_elf_hash_table (htab)) 4556 { 4557 /* Set a flag in the hash table entry indicating the type of 4558 reference or definition we just found. A dynamic symbol 4559 is one which is referenced or defined by both a regular 4560 object and a shared object. */ 4561 bfd_boolean dynsym = FALSE; 4562 4563 /* Plugin symbols aren't normal. Don't set def_regular or 4564 ref_regular for them, or make them dynamic. */ 4565 if ((abfd->flags & BFD_PLUGIN) != 0) 4566 ; 4567 else if (! dynamic) 4568 { 4569 if (! definition) 4570 { 4571 h->ref_regular = 1; 4572 if (bind != STB_WEAK) 4573 h->ref_regular_nonweak = 1; 4574 } 4575 else 4576 { 4577 h->def_regular = 1; 4578 if (h->def_dynamic) 4579 { 4580 h->def_dynamic = 0; 4581 h->ref_dynamic = 1; 4582 } 4583 } 4584 4585 /* If the indirect symbol has been forced local, don't 4586 make the real symbol dynamic. */ 4587 if ((h == hi || !hi->forced_local) 4588 && (bfd_link_dll (info) 4589 || h->def_dynamic 4590 || h->ref_dynamic)) 4591 dynsym = TRUE; 4592 } 4593 else 4594 { 4595 if (! definition) 4596 { 4597 h->ref_dynamic = 1; 4598 hi->ref_dynamic = 1; 4599 } 4600 else 4601 { 4602 h->def_dynamic = 1; 4603 hi->def_dynamic = 1; 4604 } 4605 4606 /* If the indirect symbol has been forced local, don't 4607 make the real symbol dynamic. */ 4608 if ((h == hi || !hi->forced_local) 4609 && (h->def_regular 4610 || h->ref_regular 4611 || (h->u.weakdef != NULL 4612 && ! new_weakdef 4613 && h->u.weakdef->dynindx != -1))) 4614 dynsym = TRUE; 4615 } 4616 4617 /* Check to see if we need to add an indirect symbol for 4618 the default name. */ 4619 if (definition 4620 || (!override && h->root.type == bfd_link_hash_common)) 4621 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, 4622 sec, value, &old_bfd, &dynsym)) 4623 goto error_free_vers; 4624 4625 /* Check the alignment when a common symbol is involved. This 4626 can change when a common symbol is overridden by a normal 4627 definition or a common symbol is ignored due to the old 4628 normal definition. We need to make sure the maximum 4629 alignment is maintained. */ 4630 if ((old_alignment || common) 4631 && h->root.type != bfd_link_hash_common) 4632 { 4633 unsigned int common_align; 4634 unsigned int normal_align; 4635 unsigned int symbol_align; 4636 bfd *normal_bfd; 4637 bfd *common_bfd; 4638 4639 BFD_ASSERT (h->root.type == bfd_link_hash_defined 4640 || h->root.type == bfd_link_hash_defweak); 4641 4642 symbol_align = ffs (h->root.u.def.value) - 1; 4643 if (h->root.u.def.section->owner != NULL 4644 && (h->root.u.def.section->owner->flags 4645 & (DYNAMIC | BFD_PLUGIN)) == 0) 4646 { 4647 normal_align = h->root.u.def.section->alignment_power; 4648 if (normal_align > symbol_align) 4649 normal_align = symbol_align; 4650 } 4651 else 4652 normal_align = symbol_align; 4653 4654 if (old_alignment) 4655 { 4656 common_align = old_alignment; 4657 common_bfd = old_bfd; 4658 normal_bfd = abfd; 4659 } 4660 else 4661 { 4662 common_align = bfd_log2 (isym->st_value); 4663 common_bfd = abfd; 4664 normal_bfd = old_bfd; 4665 } 4666 4667 if (normal_align < common_align) 4668 { 4669 /* PR binutils/2735 */ 4670 if (normal_bfd == NULL) 4671 _bfd_error_handler 4672 /* xgettext:c-format */ 4673 (_("Warning: alignment %u of common symbol `%s' in %B is" 4674 " greater than the alignment (%u) of its section %A"), 4675 common_bfd, h->root.u.def.section, 4676 1 << common_align, name, 1 << normal_align); 4677 else 4678 _bfd_error_handler 4679 /* xgettext:c-format */ 4680 (_("Warning: alignment %u of symbol `%s' in %B" 4681 " is smaller than %u in %B"), 4682 normal_bfd, common_bfd, 4683 1 << normal_align, name, 1 << common_align); 4684 } 4685 } 4686 4687 /* Remember the symbol size if it isn't undefined. */ 4688 if (isym->st_size != 0 4689 && isym->st_shndx != SHN_UNDEF 4690 && (definition || h->size == 0)) 4691 { 4692 if (h->size != 0 4693 && h->size != isym->st_size 4694 && ! size_change_ok) 4695 _bfd_error_handler 4696 /* xgettext:c-format */ 4697 (_("Warning: size of symbol `%s' changed" 4698 " from %lu in %B to %lu in %B"), 4699 old_bfd, abfd, 4700 name, (unsigned long) h->size, 4701 (unsigned long) isym->st_size); 4702 4703 h->size = isym->st_size; 4704 } 4705 4706 /* If this is a common symbol, then we always want H->SIZE 4707 to be the size of the common symbol. The code just above 4708 won't fix the size if a common symbol becomes larger. We 4709 don't warn about a size change here, because that is 4710 covered by --warn-common. Allow changes between different 4711 function types. */ 4712 if (h->root.type == bfd_link_hash_common) 4713 h->size = h->root.u.c.size; 4714 4715 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE 4716 && ((definition && !new_weak) 4717 || (old_weak && h->root.type == bfd_link_hash_common) 4718 || h->type == STT_NOTYPE)) 4719 { 4720 unsigned int type = ELF_ST_TYPE (isym->st_info); 4721 4722 /* Turn an IFUNC symbol from a DSO into a normal FUNC 4723 symbol. */ 4724 if (type == STT_GNU_IFUNC 4725 && (abfd->flags & DYNAMIC) != 0) 4726 type = STT_FUNC; 4727 4728 if (h->type != type) 4729 { 4730 if (h->type != STT_NOTYPE && ! type_change_ok) 4731 /* xgettext:c-format */ 4732 _bfd_error_handler 4733 (_("Warning: type of symbol `%s' changed" 4734 " from %d to %d in %B"), 4735 abfd, name, h->type, type); 4736 4737 h->type = type; 4738 } 4739 } 4740 4741 /* Merge st_other field. */ 4742 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); 4743 4744 /* We don't want to make debug symbol dynamic. */ 4745 if (definition 4746 && (sec->flags & SEC_DEBUGGING) 4747 && !bfd_link_relocatable (info)) 4748 dynsym = FALSE; 4749 4750 /* Nor should we make plugin symbols dynamic. */ 4751 if ((abfd->flags & BFD_PLUGIN) != 0) 4752 dynsym = FALSE; 4753 4754 if (definition) 4755 { 4756 h->target_internal = isym->st_target_internal; 4757 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; 4758 } 4759 4760 if (definition && !dynamic) 4761 { 4762 char *p = strchr (name, ELF_VER_CHR); 4763 if (p != NULL && p[1] != ELF_VER_CHR) 4764 { 4765 /* Queue non-default versions so that .symver x, x@FOO 4766 aliases can be checked. */ 4767 if (!nondeflt_vers) 4768 { 4769 amt = ((isymend - isym + 1) 4770 * sizeof (struct elf_link_hash_entry *)); 4771 nondeflt_vers 4772 = (struct elf_link_hash_entry **) bfd_malloc (amt); 4773 if (!nondeflt_vers) 4774 goto error_free_vers; 4775 } 4776 nondeflt_vers[nondeflt_vers_cnt++] = h; 4777 } 4778 } 4779 4780 if (dynsym && h->dynindx == -1) 4781 { 4782 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 4783 goto error_free_vers; 4784 if (h->u.weakdef != NULL 4785 && ! new_weakdef 4786 && h->u.weakdef->dynindx == -1) 4787 { 4788 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) 4789 goto error_free_vers; 4790 } 4791 } 4792 else if (h->dynindx != -1) 4793 /* If the symbol already has a dynamic index, but 4794 visibility says it should not be visible, turn it into 4795 a local symbol. */ 4796 switch (ELF_ST_VISIBILITY (h->other)) 4797 { 4798 case STV_INTERNAL: 4799 case STV_HIDDEN: 4800 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 4801 dynsym = FALSE; 4802 break; 4803 } 4804 4805 /* Don't add DT_NEEDED for references from the dummy bfd nor 4806 for unmatched symbol. */ 4807 if (!add_needed 4808 && matched 4809 && definition 4810 && ((dynsym 4811 && h->ref_regular_nonweak 4812 && (old_bfd == NULL 4813 || (old_bfd->flags & BFD_PLUGIN) == 0)) 4814 || (h->ref_dynamic_nonweak 4815 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 4816 && !on_needed_list (elf_dt_name (abfd), 4817 htab->needed, NULL)))) 4818 { 4819 int ret; 4820 const char *soname = elf_dt_name (abfd); 4821 4822 info->callbacks->minfo ("%!", soname, old_bfd, 4823 h->root.root.string); 4824 4825 /* A symbol from a library loaded via DT_NEEDED of some 4826 other library is referenced by a regular object. 4827 Add a DT_NEEDED entry for it. Issue an error if 4828 --no-add-needed is used and the reference was not 4829 a weak one. */ 4830 if (old_bfd != NULL 4831 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) 4832 { 4833 _bfd_error_handler 4834 /* xgettext:c-format */ 4835 (_("%B: undefined reference to symbol '%s'"), 4836 old_bfd, name); 4837 bfd_set_error (bfd_error_missing_dso); 4838 goto error_free_vers; 4839 } 4840 4841 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) 4842 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); 4843 4844 add_needed = TRUE; 4845 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); 4846 if (ret < 0) 4847 goto error_free_vers; 4848 4849 BFD_ASSERT (ret == 0); 4850 } 4851 } 4852 } 4853 4854 if (extversym != NULL) 4855 { 4856 free (extversym); 4857 extversym = NULL; 4858 } 4859 4860 if (isymbuf != NULL) 4861 { 4862 free (isymbuf); 4863 isymbuf = NULL; 4864 } 4865 4866 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) 4867 { 4868 unsigned int i; 4869 4870 /* Restore the symbol table. */ 4871 old_ent = (char *) old_tab + tabsize; 4872 memset (elf_sym_hashes (abfd), 0, 4873 extsymcount * sizeof (struct elf_link_hash_entry *)); 4874 htab->root.table.table = old_table; 4875 htab->root.table.size = old_size; 4876 htab->root.table.count = old_count; 4877 memcpy (htab->root.table.table, old_tab, tabsize); 4878 htab->root.undefs = old_undefs; 4879 htab->root.undefs_tail = old_undefs_tail; 4880 _bfd_elf_strtab_restore (htab->dynstr, old_strtab); 4881 free (old_strtab); 4882 old_strtab = NULL; 4883 for (i = 0; i < htab->root.table.size; i++) 4884 { 4885 struct bfd_hash_entry *p; 4886 struct elf_link_hash_entry *h; 4887 bfd_size_type size; 4888 unsigned int alignment_power; 4889 4890 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 4891 { 4892 h = (struct elf_link_hash_entry *) p; 4893 if (h->root.type == bfd_link_hash_warning) 4894 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4895 4896 /* Preserve the maximum alignment and size for common 4897 symbols even if this dynamic lib isn't on DT_NEEDED 4898 since it can still be loaded at run time by another 4899 dynamic lib. */ 4900 if (h->root.type == bfd_link_hash_common) 4901 { 4902 size = h->root.u.c.size; 4903 alignment_power = h->root.u.c.p->alignment_power; 4904 } 4905 else 4906 { 4907 size = 0; 4908 alignment_power = 0; 4909 } 4910 memcpy (p, old_ent, htab->root.table.entsize); 4911 old_ent = (char *) old_ent + htab->root.table.entsize; 4912 h = (struct elf_link_hash_entry *) p; 4913 if (h->root.type == bfd_link_hash_warning) 4914 { 4915 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); 4916 old_ent = (char *) old_ent + htab->root.table.entsize; 4917 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4918 } 4919 if (h->root.type == bfd_link_hash_common) 4920 { 4921 if (size > h->root.u.c.size) 4922 h->root.u.c.size = size; 4923 if (alignment_power > h->root.u.c.p->alignment_power) 4924 h->root.u.c.p->alignment_power = alignment_power; 4925 } 4926 } 4927 } 4928 4929 /* Make a special call to the linker "notice" function to 4930 tell it that symbols added for crefs may need to be removed. */ 4931 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) 4932 goto error_free_vers; 4933 4934 free (old_tab); 4935 objalloc_free_block ((struct objalloc *) htab->root.table.memory, 4936 alloc_mark); 4937 if (nondeflt_vers != NULL) 4938 free (nondeflt_vers); 4939 return TRUE; 4940 } 4941 4942 if (old_tab != NULL) 4943 { 4944 if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) 4945 goto error_free_vers; 4946 free (old_tab); 4947 old_tab = NULL; 4948 } 4949 4950 /* Now that all the symbols from this input file are created, if 4951 not performing a relocatable link, handle .symver foo, foo@BAR 4952 such that any relocs against foo become foo@BAR. */ 4953 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) 4954 { 4955 size_t cnt, symidx; 4956 4957 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) 4958 { 4959 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; 4960 char *shortname, *p; 4961 4962 p = strchr (h->root.root.string, ELF_VER_CHR); 4963 if (p == NULL 4964 || (h->root.type != bfd_link_hash_defined 4965 && h->root.type != bfd_link_hash_defweak)) 4966 continue; 4967 4968 amt = p - h->root.root.string; 4969 shortname = (char *) bfd_malloc (amt + 1); 4970 if (!shortname) 4971 goto error_free_vers; 4972 memcpy (shortname, h->root.root.string, amt); 4973 shortname[amt] = '\0'; 4974 4975 hi = (struct elf_link_hash_entry *) 4976 bfd_link_hash_lookup (&htab->root, shortname, 4977 FALSE, FALSE, FALSE); 4978 if (hi != NULL 4979 && hi->root.type == h->root.type 4980 && hi->root.u.def.value == h->root.u.def.value 4981 && hi->root.u.def.section == h->root.u.def.section) 4982 { 4983 (*bed->elf_backend_hide_symbol) (info, hi, TRUE); 4984 hi->root.type = bfd_link_hash_indirect; 4985 hi->root.u.i.link = (struct bfd_link_hash_entry *) h; 4986 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); 4987 sym_hash = elf_sym_hashes (abfd); 4988 if (sym_hash) 4989 for (symidx = 0; symidx < extsymcount; ++symidx) 4990 if (sym_hash[symidx] == hi) 4991 { 4992 sym_hash[symidx] = h; 4993 break; 4994 } 4995 } 4996 free (shortname); 4997 } 4998 free (nondeflt_vers); 4999 nondeflt_vers = NULL; 5000 } 5001 5002 /* Now set the weakdefs field correctly for all the weak defined 5003 symbols we found. The only way to do this is to search all the 5004 symbols. Since we only need the information for non functions in 5005 dynamic objects, that's the only time we actually put anything on 5006 the list WEAKS. We need this information so that if a regular 5007 object refers to a symbol defined weakly in a dynamic object, the 5008 real symbol in the dynamic object is also put in the dynamic 5009 symbols; we also must arrange for both symbols to point to the 5010 same memory location. We could handle the general case of symbol 5011 aliasing, but a general symbol alias can only be generated in 5012 assembler code, handling it correctly would be very time 5013 consuming, and other ELF linkers don't handle general aliasing 5014 either. */ 5015 if (weaks != NULL) 5016 { 5017 struct elf_link_hash_entry **hpp; 5018 struct elf_link_hash_entry **hppend; 5019 struct elf_link_hash_entry **sorted_sym_hash; 5020 struct elf_link_hash_entry *h; 5021 size_t sym_count; 5022 5023 /* Since we have to search the whole symbol list for each weak 5024 defined symbol, search time for N weak defined symbols will be 5025 O(N^2). Binary search will cut it down to O(NlogN). */ 5026 amt = extsymcount; 5027 amt *= sizeof (struct elf_link_hash_entry *); 5028 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); 5029 if (sorted_sym_hash == NULL) 5030 goto error_return; 5031 sym_hash = sorted_sym_hash; 5032 hpp = elf_sym_hashes (abfd); 5033 hppend = hpp + extsymcount; 5034 sym_count = 0; 5035 for (; hpp < hppend; hpp++) 5036 { 5037 h = *hpp; 5038 if (h != NULL 5039 && h->root.type == bfd_link_hash_defined 5040 && !bed->is_function_type (h->type)) 5041 { 5042 *sym_hash = h; 5043 sym_hash++; 5044 sym_count++; 5045 } 5046 } 5047 5048 qsort (sorted_sym_hash, sym_count, 5049 sizeof (struct elf_link_hash_entry *), 5050 elf_sort_symbol); 5051 5052 while (weaks != NULL) 5053 { 5054 struct elf_link_hash_entry *hlook; 5055 asection *slook; 5056 bfd_vma vlook; 5057 size_t i, j, idx = 0; 5058 5059 hlook = weaks; 5060 weaks = hlook->u.weakdef; 5061 hlook->u.weakdef = NULL; 5062 5063 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined 5064 || hlook->root.type == bfd_link_hash_defweak 5065 || hlook->root.type == bfd_link_hash_common 5066 || hlook->root.type == bfd_link_hash_indirect); 5067 slook = hlook->root.u.def.section; 5068 vlook = hlook->root.u.def.value; 5069 5070 i = 0; 5071 j = sym_count; 5072 while (i != j) 5073 { 5074 bfd_signed_vma vdiff; 5075 idx = (i + j) / 2; 5076 h = sorted_sym_hash[idx]; 5077 vdiff = vlook - h->root.u.def.value; 5078 if (vdiff < 0) 5079 j = idx; 5080 else if (vdiff > 0) 5081 i = idx + 1; 5082 else 5083 { 5084 int sdiff = slook->id - h->root.u.def.section->id; 5085 if (sdiff < 0) 5086 j = idx; 5087 else if (sdiff > 0) 5088 i = idx + 1; 5089 else 5090 break; 5091 } 5092 } 5093 5094 /* We didn't find a value/section match. */ 5095 if (i == j) 5096 continue; 5097 5098 /* With multiple aliases, or when the weak symbol is already 5099 strongly defined, we have multiple matching symbols and 5100 the binary search above may land on any of them. Step 5101 one past the matching symbol(s). */ 5102 while (++idx != j) 5103 { 5104 h = sorted_sym_hash[idx]; 5105 if (h->root.u.def.section != slook 5106 || h->root.u.def.value != vlook) 5107 break; 5108 } 5109 5110 /* Now look back over the aliases. Since we sorted by size 5111 as well as value and section, we'll choose the one with 5112 the largest size. */ 5113 while (idx-- != i) 5114 { 5115 h = sorted_sym_hash[idx]; 5116 5117 /* Stop if value or section doesn't match. */ 5118 if (h->root.u.def.section != slook 5119 || h->root.u.def.value != vlook) 5120 break; 5121 else if (h != hlook) 5122 { 5123 hlook->u.weakdef = h; 5124 5125 /* If the weak definition is in the list of dynamic 5126 symbols, make sure the real definition is put 5127 there as well. */ 5128 if (hlook->dynindx != -1 && h->dynindx == -1) 5129 { 5130 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 5131 { 5132 err_free_sym_hash: 5133 free (sorted_sym_hash); 5134 goto error_return; 5135 } 5136 } 5137 5138 /* If the real definition is in the list of dynamic 5139 symbols, make sure the weak definition is put 5140 there as well. If we don't do this, then the 5141 dynamic loader might not merge the entries for the 5142 real definition and the weak definition. */ 5143 if (h->dynindx != -1 && hlook->dynindx == -1) 5144 { 5145 if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) 5146 goto err_free_sym_hash; 5147 } 5148 break; 5149 } 5150 } 5151 } 5152 5153 free (sorted_sym_hash); 5154 } 5155 5156 if (bed->check_directives 5157 && !(*bed->check_directives) (abfd, info)) 5158 return FALSE; 5159 5160 if (!info->check_relocs_after_open_input 5161 && !_bfd_elf_link_check_relocs (abfd, info)) 5162 return FALSE; 5163 5164 /* If this is a non-traditional link, try to optimize the handling 5165 of the .stab/.stabstr sections. */ 5166 if (! dynamic 5167 && ! info->traditional_format 5168 && is_elf_hash_table (htab) 5169 && (info->strip != strip_all && info->strip != strip_debugger)) 5170 { 5171 asection *stabstr; 5172 5173 stabstr = bfd_get_section_by_name (abfd, ".stabstr"); 5174 if (stabstr != NULL) 5175 { 5176 bfd_size_type string_offset = 0; 5177 asection *stab; 5178 5179 for (stab = abfd->sections; stab; stab = stab->next) 5180 if (CONST_STRNEQ (stab->name, ".stab") 5181 && (!stab->name[5] || 5182 (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) 5183 && (stab->flags & SEC_MERGE) == 0 5184 && !bfd_is_abs_section (stab->output_section)) 5185 { 5186 struct bfd_elf_section_data *secdata; 5187 5188 secdata = elf_section_data (stab); 5189 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, 5190 stabstr, &secdata->sec_info, 5191 &string_offset)) 5192 goto error_return; 5193 if (secdata->sec_info) 5194 stab->sec_info_type = SEC_INFO_TYPE_STABS; 5195 } 5196 } 5197 } 5198 5199 if (is_elf_hash_table (htab) && add_needed) 5200 { 5201 /* Add this bfd to the loaded list. */ 5202 struct elf_link_loaded_list *n; 5203 5204 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); 5205 if (n == NULL) 5206 goto error_return; 5207 n->abfd = abfd; 5208 n->next = htab->loaded; 5209 htab->loaded = n; 5210 } 5211 5212 return TRUE; 5213 5214 error_free_vers: 5215 if (old_tab != NULL) 5216 free (old_tab); 5217 if (old_strtab != NULL) 5218 free (old_strtab); 5219 if (nondeflt_vers != NULL) 5220 free (nondeflt_vers); 5221 if (extversym != NULL) 5222 free (extversym); 5223 error_free_sym: 5224 if (isymbuf != NULL) 5225 free (isymbuf); 5226 error_return: 5227 return FALSE; 5228} 5229 5230/* Return the linker hash table entry of a symbol that might be 5231 satisfied by an archive symbol. Return -1 on error. */ 5232 5233struct elf_link_hash_entry * 5234_bfd_elf_archive_symbol_lookup (bfd *abfd, 5235 struct bfd_link_info *info, 5236 const char *name) 5237{ 5238 struct elf_link_hash_entry *h; 5239 char *p, *copy; 5240 size_t len, first; 5241 5242 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); 5243 if (h != NULL) 5244 return h; 5245 5246 /* If this is a default version (the name contains @@), look up the 5247 symbol again with only one `@' as well as without the version. 5248 The effect is that references to the symbol with and without the 5249 version will be matched by the default symbol in the archive. */ 5250 5251 p = strchr (name, ELF_VER_CHR); 5252 if (p == NULL || p[1] != ELF_VER_CHR) 5253 return h; 5254 5255 /* First check with only one `@'. */ 5256 len = strlen (name); 5257 copy = (char *) bfd_alloc (abfd, len); 5258 if (copy == NULL) 5259 return (struct elf_link_hash_entry *) 0 - 1; 5260 5261 first = p - name + 1; 5262 memcpy (copy, name, first); 5263 memcpy (copy + first, name + first + 1, len - first); 5264 5265 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); 5266 if (h == NULL) 5267 { 5268 /* We also need to check references to the symbol without the 5269 version. */ 5270 copy[first - 1] = '\0'; 5271 h = elf_link_hash_lookup (elf_hash_table (info), copy, 5272 FALSE, FALSE, TRUE); 5273 } 5274 5275 bfd_release (abfd, copy); 5276 return h; 5277} 5278 5279/* Add symbols from an ELF archive file to the linker hash table. We 5280 don't use _bfd_generic_link_add_archive_symbols because we need to 5281 handle versioned symbols. 5282 5283 Fortunately, ELF archive handling is simpler than that done by 5284 _bfd_generic_link_add_archive_symbols, which has to allow for a.out 5285 oddities. In ELF, if we find a symbol in the archive map, and the 5286 symbol is currently undefined, we know that we must pull in that 5287 object file. 5288 5289 Unfortunately, we do have to make multiple passes over the symbol 5290 table until nothing further is resolved. */ 5291 5292static bfd_boolean 5293elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) 5294{ 5295 symindex c; 5296 unsigned char *included = NULL; 5297 carsym *symdefs; 5298 bfd_boolean loop; 5299 bfd_size_type amt; 5300 const struct elf_backend_data *bed; 5301 struct elf_link_hash_entry * (*archive_symbol_lookup) 5302 (bfd *, struct bfd_link_info *, const char *); 5303 5304 if (! bfd_has_map (abfd)) 5305 { 5306 /* An empty archive is a special case. */ 5307 if (bfd_openr_next_archived_file (abfd, NULL) == NULL) 5308 return TRUE; 5309 bfd_set_error (bfd_error_no_armap); 5310 return FALSE; 5311 } 5312 5313 /* Keep track of all symbols we know to be already defined, and all 5314 files we know to be already included. This is to speed up the 5315 second and subsequent passes. */ 5316 c = bfd_ardata (abfd)->symdef_count; 5317 if (c == 0) 5318 return TRUE; 5319 amt = c; 5320 amt *= sizeof (*included); 5321 included = (unsigned char *) bfd_zmalloc (amt); 5322 if (included == NULL) 5323 return FALSE; 5324 5325 symdefs = bfd_ardata (abfd)->symdefs; 5326 bed = get_elf_backend_data (abfd); 5327 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; 5328 5329 do 5330 { 5331 file_ptr last; 5332 symindex i; 5333 carsym *symdef; 5334 carsym *symdefend; 5335 5336 loop = FALSE; 5337 last = -1; 5338 5339 symdef = symdefs; 5340 symdefend = symdef + c; 5341 for (i = 0; symdef < symdefend; symdef++, i++) 5342 { 5343 struct elf_link_hash_entry *h; 5344 bfd *element; 5345 struct bfd_link_hash_entry *undefs_tail; 5346 symindex mark; 5347 5348 if (included[i]) 5349 continue; 5350 if (symdef->file_offset == last) 5351 { 5352 included[i] = TRUE; 5353 continue; 5354 } 5355 5356 h = archive_symbol_lookup (abfd, info, symdef->name); 5357 if (h == (struct elf_link_hash_entry *) 0 - 1) 5358 goto error_return; 5359 5360 if (h == NULL) 5361 continue; 5362 5363 if (h->root.type == bfd_link_hash_common) 5364 { 5365 /* We currently have a common symbol. The archive map contains 5366 a reference to this symbol, so we may want to include it. We 5367 only want to include it however, if this archive element 5368 contains a definition of the symbol, not just another common 5369 declaration of it. 5370 5371 Unfortunately some archivers (including GNU ar) will put 5372 declarations of common symbols into their archive maps, as 5373 well as real definitions, so we cannot just go by the archive 5374 map alone. Instead we must read in the element's symbol 5375 table and check that to see what kind of symbol definition 5376 this is. */ 5377 if (! elf_link_is_defined_archive_symbol (abfd, symdef)) 5378 continue; 5379 } 5380 else if (h->root.type != bfd_link_hash_undefined) 5381 { 5382 if (h->root.type != bfd_link_hash_undefweak) 5383 /* Symbol must be defined. Don't check it again. */ 5384 included[i] = TRUE; 5385 continue; 5386 } 5387 5388 /* We need to include this archive member. */ 5389 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); 5390 if (element == NULL) 5391 goto error_return; 5392 5393 if (! bfd_check_format (element, bfd_object)) 5394 goto error_return; 5395 5396 undefs_tail = info->hash->undefs_tail; 5397 5398 if (!(*info->callbacks 5399 ->add_archive_element) (info, element, symdef->name, &element)) 5400 continue; 5401 if (!bfd_link_add_symbols (element, info)) 5402 goto error_return; 5403 5404 /* If there are any new undefined symbols, we need to make 5405 another pass through the archive in order to see whether 5406 they can be defined. FIXME: This isn't perfect, because 5407 common symbols wind up on undefs_tail and because an 5408 undefined symbol which is defined later on in this pass 5409 does not require another pass. This isn't a bug, but it 5410 does make the code less efficient than it could be. */ 5411 if (undefs_tail != info->hash->undefs_tail) 5412 loop = TRUE; 5413 5414 /* Look backward to mark all symbols from this object file 5415 which we have already seen in this pass. */ 5416 mark = i; 5417 do 5418 { 5419 included[mark] = TRUE; 5420 if (mark == 0) 5421 break; 5422 --mark; 5423 } 5424 while (symdefs[mark].file_offset == symdef->file_offset); 5425 5426 /* We mark subsequent symbols from this object file as we go 5427 on through the loop. */ 5428 last = symdef->file_offset; 5429 } 5430 } 5431 while (loop); 5432 5433 free (included); 5434 5435 return TRUE; 5436 5437 error_return: 5438 if (included != NULL) 5439 free (included); 5440 return FALSE; 5441} 5442 5443/* Given an ELF BFD, add symbols to the global hash table as 5444 appropriate. */ 5445 5446bfd_boolean 5447bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) 5448{ 5449 switch (bfd_get_format (abfd)) 5450 { 5451 case bfd_object: 5452 return elf_link_add_object_symbols (abfd, info); 5453 case bfd_archive: 5454 return elf_link_add_archive_symbols (abfd, info); 5455 default: 5456 bfd_set_error (bfd_error_wrong_format); 5457 return FALSE; 5458 } 5459} 5460 5461struct hash_codes_info 5462{ 5463 unsigned long *hashcodes; 5464 bfd_boolean error; 5465}; 5466 5467/* This function will be called though elf_link_hash_traverse to store 5468 all hash value of the exported symbols in an array. */ 5469 5470static bfd_boolean 5471elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) 5472{ 5473 struct hash_codes_info *inf = (struct hash_codes_info *) data; 5474 const char *name; 5475 unsigned long ha; 5476 char *alc = NULL; 5477 5478 /* Ignore indirect symbols. These are added by the versioning code. */ 5479 if (h->dynindx == -1) 5480 return TRUE; 5481 5482 name = h->root.root.string; 5483 if (h->versioned >= versioned) 5484 { 5485 char *p = strchr (name, ELF_VER_CHR); 5486 if (p != NULL) 5487 { 5488 alc = (char *) bfd_malloc (p - name + 1); 5489 if (alc == NULL) 5490 { 5491 inf->error = TRUE; 5492 return FALSE; 5493 } 5494 memcpy (alc, name, p - name); 5495 alc[p - name] = '\0'; 5496 name = alc; 5497 } 5498 } 5499 5500 /* Compute the hash value. */ 5501 ha = bfd_elf_hash (name); 5502 5503 /* Store the found hash value in the array given as the argument. */ 5504 *(inf->hashcodes)++ = ha; 5505 5506 /* And store it in the struct so that we can put it in the hash table 5507 later. */ 5508 h->u.elf_hash_value = ha; 5509 5510 if (alc != NULL) 5511 free (alc); 5512 5513 return TRUE; 5514} 5515 5516struct collect_gnu_hash_codes 5517{ 5518 bfd *output_bfd; 5519 const struct elf_backend_data *bed; 5520 unsigned long int nsyms; 5521 unsigned long int maskbits; 5522 unsigned long int *hashcodes; 5523 unsigned long int *hashval; 5524 unsigned long int *indx; 5525 unsigned long int *counts; 5526 bfd_vma *bitmask; 5527 bfd_byte *contents; 5528 long int min_dynindx; 5529 unsigned long int bucketcount; 5530 unsigned long int symindx; 5531 long int local_indx; 5532 long int shift1, shift2; 5533 unsigned long int mask; 5534 bfd_boolean error; 5535}; 5536 5537/* This function will be called though elf_link_hash_traverse to store 5538 all hash value of the exported symbols in an array. */ 5539 5540static bfd_boolean 5541elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) 5542{ 5543 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; 5544 const char *name; 5545 unsigned long ha; 5546 char *alc = NULL; 5547 5548 /* Ignore indirect symbols. These are added by the versioning code. */ 5549 if (h->dynindx == -1) 5550 return TRUE; 5551 5552 /* Ignore also local symbols and undefined symbols. */ 5553 if (! (*s->bed->elf_hash_symbol) (h)) 5554 return TRUE; 5555 5556 name = h->root.root.string; 5557 if (h->versioned >= versioned) 5558 { 5559 char *p = strchr (name, ELF_VER_CHR); 5560 if (p != NULL) 5561 { 5562 alc = (char *) bfd_malloc (p - name + 1); 5563 if (alc == NULL) 5564 { 5565 s->error = TRUE; 5566 return FALSE; 5567 } 5568 memcpy (alc, name, p - name); 5569 alc[p - name] = '\0'; 5570 name = alc; 5571 } 5572 } 5573 5574 /* Compute the hash value. */ 5575 ha = bfd_elf_gnu_hash (name); 5576 5577 /* Store the found hash value in the array for compute_bucket_count, 5578 and also for .dynsym reordering purposes. */ 5579 s->hashcodes[s->nsyms] = ha; 5580 s->hashval[h->dynindx] = ha; 5581 ++s->nsyms; 5582 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) 5583 s->min_dynindx = h->dynindx; 5584 5585 if (alc != NULL) 5586 free (alc); 5587 5588 return TRUE; 5589} 5590 5591/* This function will be called though elf_link_hash_traverse to do 5592 final dynaminc symbol renumbering. */ 5593 5594static bfd_boolean 5595elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) 5596{ 5597 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; 5598 unsigned long int bucket; 5599 unsigned long int val; 5600 5601 /* Ignore indirect symbols. */ 5602 if (h->dynindx == -1) 5603 return TRUE; 5604 5605 /* Ignore also local symbols and undefined symbols. */ 5606 if (! (*s->bed->elf_hash_symbol) (h)) 5607 { 5608 if (h->dynindx >= s->min_dynindx) 5609 h->dynindx = s->local_indx++; 5610 return TRUE; 5611 } 5612 5613 bucket = s->hashval[h->dynindx] % s->bucketcount; 5614 val = (s->hashval[h->dynindx] >> s->shift1) 5615 & ((s->maskbits >> s->shift1) - 1); 5616 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); 5617 s->bitmask[val] 5618 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); 5619 val = s->hashval[h->dynindx] & ~(unsigned long int) 1; 5620 if (s->counts[bucket] == 1) 5621 /* Last element terminates the chain. */ 5622 val |= 1; 5623 bfd_put_32 (s->output_bfd, val, 5624 s->contents + (s->indx[bucket] - s->symindx) * 4); 5625 --s->counts[bucket]; 5626 h->dynindx = s->indx[bucket]++; 5627 return TRUE; 5628} 5629 5630/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ 5631 5632bfd_boolean 5633_bfd_elf_hash_symbol (struct elf_link_hash_entry *h) 5634{ 5635 return !(h->forced_local 5636 || h->root.type == bfd_link_hash_undefined 5637 || h->root.type == bfd_link_hash_undefweak 5638 || ((h->root.type == bfd_link_hash_defined 5639 || h->root.type == bfd_link_hash_defweak) 5640 && h->root.u.def.section->output_section == NULL)); 5641} 5642 5643/* Array used to determine the number of hash table buckets to use 5644 based on the number of symbols there are. If there are fewer than 5645 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, 5646 fewer than 37 we use 17 buckets, and so forth. We never use more 5647 than 32771 buckets. */ 5648 5649static const size_t elf_buckets[] = 5650{ 5651 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, 5652 16411, 32771, 0 5653}; 5654 5655/* Compute bucket count for hashing table. We do not use a static set 5656 of possible tables sizes anymore. Instead we determine for all 5657 possible reasonable sizes of the table the outcome (i.e., the 5658 number of collisions etc) and choose the best solution. The 5659 weighting functions are not too simple to allow the table to grow 5660 without bounds. Instead one of the weighting factors is the size. 5661 Therefore the result is always a good payoff between few collisions 5662 (= short chain lengths) and table size. */ 5663static size_t 5664compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, 5665 unsigned long int *hashcodes ATTRIBUTE_UNUSED, 5666 unsigned long int nsyms, 5667 int gnu_hash) 5668{ 5669 size_t best_size = 0; 5670 unsigned long int i; 5671 5672 /* We have a problem here. The following code to optimize the table 5673 size requires an integer type with more the 32 bits. If 5674 BFD_HOST_U_64_BIT is set we know about such a type. */ 5675#ifdef BFD_HOST_U_64_BIT 5676 if (info->optimize) 5677 { 5678 size_t minsize; 5679 size_t maxsize; 5680 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); 5681 bfd *dynobj = elf_hash_table (info)->dynobj; 5682 size_t dynsymcount = elf_hash_table (info)->dynsymcount; 5683 const struct elf_backend_data *bed = get_elf_backend_data (dynobj); 5684 unsigned long int *counts; 5685 bfd_size_type amt; 5686 unsigned int no_improvement_count = 0; 5687 5688 /* Possible optimization parameters: if we have NSYMS symbols we say 5689 that the hashing table must at least have NSYMS/4 and at most 5690 2*NSYMS buckets. */ 5691 minsize = nsyms / 4; 5692 if (minsize == 0) 5693 minsize = 1; 5694 best_size = maxsize = nsyms * 2; 5695 if (gnu_hash) 5696 { 5697 if (minsize < 2) 5698 minsize = 2; 5699 if ((best_size & 31) == 0) 5700 ++best_size; 5701 } 5702 5703 /* Create array where we count the collisions in. We must use bfd_malloc 5704 since the size could be large. */ 5705 amt = maxsize; 5706 amt *= sizeof (unsigned long int); 5707 counts = (unsigned long int *) bfd_malloc (amt); 5708 if (counts == NULL) 5709 return 0; 5710 5711 /* Compute the "optimal" size for the hash table. The criteria is a 5712 minimal chain length. The minor criteria is (of course) the size 5713 of the table. */ 5714 for (i = minsize; i < maxsize; ++i) 5715 { 5716 /* Walk through the array of hashcodes and count the collisions. */ 5717 BFD_HOST_U_64_BIT max; 5718 unsigned long int j; 5719 unsigned long int fact; 5720 5721 if (gnu_hash && (i & 31) == 0) 5722 continue; 5723 5724 memset (counts, '\0', i * sizeof (unsigned long int)); 5725 5726 /* Determine how often each hash bucket is used. */ 5727 for (j = 0; j < nsyms; ++j) 5728 ++counts[hashcodes[j] % i]; 5729 5730 /* For the weight function we need some information about the 5731 pagesize on the target. This is information need not be 100% 5732 accurate. Since this information is not available (so far) we 5733 define it here to a reasonable default value. If it is crucial 5734 to have a better value some day simply define this value. */ 5735# ifndef BFD_TARGET_PAGESIZE 5736# define BFD_TARGET_PAGESIZE (4096) 5737# endif 5738 5739 /* We in any case need 2 + DYNSYMCOUNT entries for the size values 5740 and the chains. */ 5741 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; 5742 5743# if 1 5744 /* Variant 1: optimize for short chains. We add the squares 5745 of all the chain lengths (which favors many small chain 5746 over a few long chains). */ 5747 for (j = 0; j < i; ++j) 5748 max += counts[j] * counts[j]; 5749 5750 /* This adds penalties for the overall size of the table. */ 5751 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; 5752 max *= fact * fact; 5753# else 5754 /* Variant 2: Optimize a lot more for small table. Here we 5755 also add squares of the size but we also add penalties for 5756 empty slots (the +1 term). */ 5757 for (j = 0; j < i; ++j) 5758 max += (1 + counts[j]) * (1 + counts[j]); 5759 5760 /* The overall size of the table is considered, but not as 5761 strong as in variant 1, where it is squared. */ 5762 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; 5763 max *= fact; 5764# endif 5765 5766 /* Compare with current best results. */ 5767 if (max < best_chlen) 5768 { 5769 best_chlen = max; 5770 best_size = i; 5771 no_improvement_count = 0; 5772 } 5773 /* PR 11843: Avoid futile long searches for the best bucket size 5774 when there are a large number of symbols. */ 5775 else if (++no_improvement_count == 100) 5776 break; 5777 } 5778 5779 free (counts); 5780 } 5781 else 5782#endif /* defined (BFD_HOST_U_64_BIT) */ 5783 { 5784 /* This is the fallback solution if no 64bit type is available or if we 5785 are not supposed to spend much time on optimizations. We select the 5786 bucket count using a fixed set of numbers. */ 5787 for (i = 0; elf_buckets[i] != 0; i++) 5788 { 5789 best_size = elf_buckets[i]; 5790 if (nsyms < elf_buckets[i + 1]) 5791 break; 5792 } 5793 if (gnu_hash && best_size < 2) 5794 best_size = 2; 5795 } 5796 5797 return best_size; 5798} 5799 5800/* Size any SHT_GROUP section for ld -r. */ 5801 5802bfd_boolean 5803_bfd_elf_size_group_sections (struct bfd_link_info *info) 5804{ 5805 bfd *ibfd; 5806 5807 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 5808 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour 5809 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) 5810 return FALSE; 5811 return TRUE; 5812} 5813 5814/* Set a default stack segment size. The value in INFO wins. If it 5815 is unset, LEGACY_SYMBOL's value is used, and if that symbol is 5816 undefined it is initialized. */ 5817 5818bfd_boolean 5819bfd_elf_stack_segment_size (bfd *output_bfd, 5820 struct bfd_link_info *info, 5821 const char *legacy_symbol, 5822 bfd_vma default_size) 5823{ 5824 struct elf_link_hash_entry *h = NULL; 5825 5826 /* Look for legacy symbol. */ 5827 if (legacy_symbol) 5828 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, 5829 FALSE, FALSE, FALSE); 5830 if (h && (h->root.type == bfd_link_hash_defined 5831 || h->root.type == bfd_link_hash_defweak) 5832 && h->def_regular 5833 && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) 5834 { 5835 /* The symbol has no type if specified on the command line. */ 5836 h->type = STT_OBJECT; 5837 if (info->stacksize) 5838 /* xgettext:c-format */ 5839 _bfd_error_handler (_("%B: stack size specified and %s set"), 5840 output_bfd, legacy_symbol); 5841 else if (h->root.u.def.section != bfd_abs_section_ptr) 5842 /* xgettext:c-format */ 5843 _bfd_error_handler (_("%B: %s not absolute"), 5844 output_bfd, legacy_symbol); 5845 else 5846 info->stacksize = h->root.u.def.value; 5847 } 5848 5849 if (!info->stacksize) 5850 /* If the user didn't set a size, or explicitly inhibit the 5851 size, set it now. */ 5852 info->stacksize = default_size; 5853 5854 /* Provide the legacy symbol, if it is referenced. */ 5855 if (h && (h->root.type == bfd_link_hash_undefined 5856 || h->root.type == bfd_link_hash_undefweak)) 5857 { 5858 struct bfd_link_hash_entry *bh = NULL; 5859 5860 if (!(_bfd_generic_link_add_one_symbol 5861 (info, output_bfd, legacy_symbol, 5862 BSF_GLOBAL, bfd_abs_section_ptr, 5863 info->stacksize >= 0 ? info->stacksize : 0, 5864 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) 5865 return FALSE; 5866 5867 h = (struct elf_link_hash_entry *) bh; 5868 h->def_regular = 1; 5869 h->type = STT_OBJECT; 5870 } 5871 5872 return TRUE; 5873} 5874 5875/* Set up the sizes and contents of the ELF dynamic sections. This is 5876 called by the ELF linker emulation before_allocation routine. We 5877 must set the sizes of the sections before the linker sets the 5878 addresses of the various sections. */ 5879 5880bfd_boolean 5881bfd_elf_size_dynamic_sections (bfd *output_bfd, 5882 const char *soname, 5883 const char *rpath, 5884 const char *filter_shlib, 5885 const char *audit, 5886 const char *depaudit, 5887 const char * const *auxiliary_filters, 5888 struct bfd_link_info *info, 5889 asection **sinterpptr) 5890{ 5891 size_t soname_indx; 5892 bfd *dynobj; 5893 const struct elf_backend_data *bed; 5894 struct elf_info_failed asvinfo; 5895 5896 *sinterpptr = NULL; 5897 5898 soname_indx = (size_t) -1; 5899 5900 if (!is_elf_hash_table (info->hash)) 5901 return TRUE; 5902 5903 bed = get_elf_backend_data (output_bfd); 5904 5905 /* Any syms created from now on start with -1 in 5906 got.refcount/offset and plt.refcount/offset. */ 5907 elf_hash_table (info)->init_got_refcount 5908 = elf_hash_table (info)->init_got_offset; 5909 elf_hash_table (info)->init_plt_refcount 5910 = elf_hash_table (info)->init_plt_offset; 5911 5912 if (bfd_link_relocatable (info) 5913 && !_bfd_elf_size_group_sections (info)) 5914 return FALSE; 5915 5916 /* The backend may have to create some sections regardless of whether 5917 we're dynamic or not. */ 5918 if (bed->elf_backend_always_size_sections 5919 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) 5920 return FALSE; 5921 5922 /* Determine any GNU_STACK segment requirements, after the backend 5923 has had a chance to set a default segment size. */ 5924 if (info->execstack) 5925 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; 5926 else if (info->noexecstack) 5927 elf_stack_flags (output_bfd) = PF_R | PF_W; 5928 else 5929 { 5930 bfd *inputobj; 5931 asection *notesec = NULL; 5932 int exec = 0; 5933 5934 for (inputobj = info->input_bfds; 5935 inputobj; 5936 inputobj = inputobj->link.next) 5937 { 5938 asection *s; 5939 5940 if (inputobj->flags 5941 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) 5942 continue; 5943 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); 5944 if (s) 5945 { 5946 if (s->flags & SEC_CODE) 5947 exec = PF_X; 5948 notesec = s; 5949 } 5950 else if (bed->default_execstack) 5951 exec = PF_X; 5952 } 5953 if (notesec || info->stacksize > 0) 5954 elf_stack_flags (output_bfd) = PF_R | PF_W | exec; 5955 if (notesec && exec && bfd_link_relocatable (info) 5956 && notesec->output_section != bfd_abs_section_ptr) 5957 notesec->output_section->flags |= SEC_CODE; 5958 } 5959 5960 dynobj = elf_hash_table (info)->dynobj; 5961 5962 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 5963 { 5964 struct elf_info_failed eif; 5965 struct elf_link_hash_entry *h; 5966 asection *dynstr; 5967 struct bfd_elf_version_tree *t; 5968 struct bfd_elf_version_expr *d; 5969 asection *s; 5970 bfd_boolean all_defined; 5971 5972 *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); 5973 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); 5974 5975 if (soname != NULL) 5976 { 5977 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 5978 soname, TRUE); 5979 if (soname_indx == (size_t) -1 5980 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) 5981 return FALSE; 5982 } 5983 5984 if (info->symbolic) 5985 { 5986 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) 5987 return FALSE; 5988 info->flags |= DF_SYMBOLIC; 5989 } 5990 5991 if (rpath != NULL) 5992 { 5993 size_t indx; 5994 bfd_vma tag; 5995 5996 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, 5997 TRUE); 5998 if (indx == (size_t) -1) 5999 return FALSE; 6000 6001 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; 6002 if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) 6003 return FALSE; 6004 } 6005 6006 if (filter_shlib != NULL) 6007 { 6008 size_t indx; 6009 6010 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6011 filter_shlib, TRUE); 6012 if (indx == (size_t) -1 6013 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) 6014 return FALSE; 6015 } 6016 6017 if (auxiliary_filters != NULL) 6018 { 6019 const char * const *p; 6020 6021 for (p = auxiliary_filters; *p != NULL; p++) 6022 { 6023 size_t indx; 6024 6025 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6026 *p, TRUE); 6027 if (indx == (size_t) -1 6028 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) 6029 return FALSE; 6030 } 6031 } 6032 6033 if (audit != NULL) 6034 { 6035 size_t indx; 6036 6037 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, 6038 TRUE); 6039 if (indx == (size_t) -1 6040 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) 6041 return FALSE; 6042 } 6043 6044 if (depaudit != NULL) 6045 { 6046 size_t indx; 6047 6048 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, 6049 TRUE); 6050 if (indx == (size_t) -1 6051 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) 6052 return FALSE; 6053 } 6054 6055 eif.info = info; 6056 eif.failed = FALSE; 6057 6058 /* If we are supposed to export all symbols into the dynamic symbol 6059 table (this is not the normal case), then do so. */ 6060 if (info->export_dynamic 6061 || (bfd_link_executable (info) && info->dynamic)) 6062 { 6063 elf_link_hash_traverse (elf_hash_table (info), 6064 _bfd_elf_export_symbol, 6065 &eif); 6066 if (eif.failed) 6067 return FALSE; 6068 } 6069 6070 /* Make all global versions with definition. */ 6071 for (t = info->version_info; t != NULL; t = t->next) 6072 for (d = t->globals.list; d != NULL; d = d->next) 6073 if (!d->symver && d->literal) 6074 { 6075 const char *verstr, *name; 6076 size_t namelen, verlen, newlen; 6077 char *newname, *p, leading_char; 6078 struct elf_link_hash_entry *newh; 6079 6080 leading_char = bfd_get_symbol_leading_char (output_bfd); 6081 name = d->pattern; 6082 namelen = strlen (name) + (leading_char != '\0'); 6083 verstr = t->name; 6084 verlen = strlen (verstr); 6085 newlen = namelen + verlen + 3; 6086 6087 newname = (char *) bfd_malloc (newlen); 6088 if (newname == NULL) 6089 return FALSE; 6090 newname[0] = leading_char; 6091 memcpy (newname + (leading_char != '\0'), name, namelen); 6092 6093 /* Check the hidden versioned definition. */ 6094 p = newname + namelen; 6095 *p++ = ELF_VER_CHR; 6096 memcpy (p, verstr, verlen + 1); 6097 newh = elf_link_hash_lookup (elf_hash_table (info), 6098 newname, FALSE, FALSE, 6099 FALSE); 6100 if (newh == NULL 6101 || (newh->root.type != bfd_link_hash_defined 6102 && newh->root.type != bfd_link_hash_defweak)) 6103 { 6104 /* Check the default versioned definition. */ 6105 *p++ = ELF_VER_CHR; 6106 memcpy (p, verstr, verlen + 1); 6107 newh = elf_link_hash_lookup (elf_hash_table (info), 6108 newname, FALSE, FALSE, 6109 FALSE); 6110 } 6111 free (newname); 6112 6113 /* Mark this version if there is a definition and it is 6114 not defined in a shared object. */ 6115 if (newh != NULL 6116 && !newh->def_dynamic 6117 && (newh->root.type == bfd_link_hash_defined 6118 || newh->root.type == bfd_link_hash_defweak)) 6119 d->symver = 1; 6120 } 6121 6122 /* Attach all the symbols to their version information. */ 6123 asvinfo.info = info; 6124 asvinfo.failed = FALSE; 6125 6126 elf_link_hash_traverse (elf_hash_table (info), 6127 _bfd_elf_link_assign_sym_version, 6128 &asvinfo); 6129 if (asvinfo.failed) 6130 return FALSE; 6131 6132 if (!info->allow_undefined_version) 6133 { 6134 /* Check if all global versions have a definition. */ 6135 all_defined = TRUE; 6136 for (t = info->version_info; t != NULL; t = t->next) 6137 for (d = t->globals.list; d != NULL; d = d->next) 6138 if (d->literal && !d->symver && !d->script) 6139 { 6140 _bfd_error_handler 6141 (_("%s: undefined version: %s"), 6142 d->pattern, t->name); 6143 all_defined = FALSE; 6144 } 6145 6146 if (!all_defined) 6147 { 6148 bfd_set_error (bfd_error_bad_value); 6149 return FALSE; 6150 } 6151 } 6152 6153 /* Find all symbols which were defined in a dynamic object and make 6154 the backend pick a reasonable value for them. */ 6155 elf_link_hash_traverse (elf_hash_table (info), 6156 _bfd_elf_adjust_dynamic_symbol, 6157 &eif); 6158 if (eif.failed) 6159 return FALSE; 6160 6161 /* Add some entries to the .dynamic section. We fill in some of the 6162 values later, in bfd_elf_final_link, but we must add the entries 6163 now so that we know the final size of the .dynamic section. */ 6164 6165 /* If there are initialization and/or finalization functions to 6166 call then add the corresponding DT_INIT/DT_FINI entries. */ 6167 h = (info->init_function 6168 ? elf_link_hash_lookup (elf_hash_table (info), 6169 info->init_function, FALSE, 6170 FALSE, FALSE) 6171 : NULL); 6172 if (h != NULL 6173 && (h->ref_regular 6174 || h->def_regular)) 6175 { 6176 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) 6177 return FALSE; 6178 } 6179 h = (info->fini_function 6180 ? elf_link_hash_lookup (elf_hash_table (info), 6181 info->fini_function, FALSE, 6182 FALSE, FALSE) 6183 : NULL); 6184 if (h != NULL 6185 && (h->ref_regular 6186 || h->def_regular)) 6187 { 6188 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) 6189 return FALSE; 6190 } 6191 6192 s = bfd_get_section_by_name (output_bfd, ".preinit_array"); 6193 if (s != NULL && s->linker_has_input) 6194 { 6195 /* DT_PREINIT_ARRAY is not allowed in shared library. */ 6196 if (! bfd_link_executable (info)) 6197 { 6198 bfd *sub; 6199 asection *o; 6200 6201 for (sub = info->input_bfds; sub != NULL; 6202 sub = sub->link.next) 6203 if (bfd_get_flavour (sub) == bfd_target_elf_flavour) 6204 for (o = sub->sections; o != NULL; o = o->next) 6205 if (elf_section_data (o)->this_hdr.sh_type 6206 == SHT_PREINIT_ARRAY) 6207 { 6208 _bfd_error_handler 6209 (_("%B: .preinit_array section is not allowed in DSO"), 6210 sub); 6211 break; 6212 } 6213 6214 bfd_set_error (bfd_error_nonrepresentable_section); 6215 return FALSE; 6216 } 6217 6218 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) 6219 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) 6220 return FALSE; 6221 } 6222 s = bfd_get_section_by_name (output_bfd, ".init_array"); 6223 if (s != NULL && s->linker_has_input) 6224 { 6225 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) 6226 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) 6227 return FALSE; 6228 } 6229 s = bfd_get_section_by_name (output_bfd, ".fini_array"); 6230 if (s != NULL && s->linker_has_input) 6231 { 6232 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) 6233 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) 6234 return FALSE; 6235 } 6236 6237 dynstr = bfd_get_linker_section (dynobj, ".dynstr"); 6238 /* If .dynstr is excluded from the link, we don't want any of 6239 these tags. Strictly, we should be checking each section 6240 individually; This quick check covers for the case where 6241 someone does a /DISCARD/ : { *(*) }. */ 6242 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) 6243 { 6244 bfd_size_type strsize; 6245 6246 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); 6247 if ((info->emit_hash 6248 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) 6249 || (info->emit_gnu_hash 6250 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) 6251 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) 6252 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) 6253 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) 6254 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, 6255 bed->s->sizeof_sym)) 6256 return FALSE; 6257 } 6258 } 6259 6260 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) 6261 return FALSE; 6262 6263 /* The backend must work out the sizes of all the other dynamic 6264 sections. */ 6265 if (dynobj != NULL 6266 && bed->elf_backend_size_dynamic_sections != NULL 6267 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) 6268 return FALSE; 6269 6270 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 6271 { 6272 unsigned long section_sym_count; 6273 struct bfd_elf_version_tree *verdefs; 6274 asection *s; 6275 6276 /* Set up the version definition section. */ 6277 s = bfd_get_linker_section (dynobj, ".gnu.version_d"); 6278 BFD_ASSERT (s != NULL); 6279 6280 /* We may have created additional version definitions if we are 6281 just linking a regular application. */ 6282 verdefs = info->version_info; 6283 6284 /* Skip anonymous version tag. */ 6285 if (verdefs != NULL && verdefs->vernum == 0) 6286 verdefs = verdefs->next; 6287 6288 if (verdefs == NULL && !info->create_default_symver) 6289 s->flags |= SEC_EXCLUDE; 6290 else 6291 { 6292 unsigned int cdefs; 6293 bfd_size_type size; 6294 struct bfd_elf_version_tree *t; 6295 bfd_byte *p; 6296 Elf_Internal_Verdef def; 6297 Elf_Internal_Verdaux defaux; 6298 struct bfd_link_hash_entry *bh; 6299 struct elf_link_hash_entry *h; 6300 const char *name; 6301 6302 cdefs = 0; 6303 size = 0; 6304 6305 /* Make space for the base version. */ 6306 size += sizeof (Elf_External_Verdef); 6307 size += sizeof (Elf_External_Verdaux); 6308 ++cdefs; 6309 6310 /* Make space for the default version. */ 6311 if (info->create_default_symver) 6312 { 6313 size += sizeof (Elf_External_Verdef); 6314 ++cdefs; 6315 } 6316 6317 for (t = verdefs; t != NULL; t = t->next) 6318 { 6319 struct bfd_elf_version_deps *n; 6320 6321 /* Don't emit base version twice. */ 6322 if (t->vernum == 0) 6323 continue; 6324 6325 size += sizeof (Elf_External_Verdef); 6326 size += sizeof (Elf_External_Verdaux); 6327 ++cdefs; 6328 6329 for (n = t->deps; n != NULL; n = n->next) 6330 size += sizeof (Elf_External_Verdaux); 6331 } 6332 6333 s->size = size; 6334 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 6335 if (s->contents == NULL && s->size != 0) 6336 return FALSE; 6337 6338 /* Fill in the version definition section. */ 6339 6340 p = s->contents; 6341 6342 def.vd_version = VER_DEF_CURRENT; 6343 def.vd_flags = VER_FLG_BASE; 6344 def.vd_ndx = 1; 6345 def.vd_cnt = 1; 6346 if (info->create_default_symver) 6347 { 6348 def.vd_aux = 2 * sizeof (Elf_External_Verdef); 6349 def.vd_next = sizeof (Elf_External_Verdef); 6350 } 6351 else 6352 { 6353 def.vd_aux = sizeof (Elf_External_Verdef); 6354 def.vd_next = (sizeof (Elf_External_Verdef) 6355 + sizeof (Elf_External_Verdaux)); 6356 } 6357 6358 if (soname_indx != (size_t) -1) 6359 { 6360 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 6361 soname_indx); 6362 def.vd_hash = bfd_elf_hash (soname); 6363 defaux.vda_name = soname_indx; 6364 name = soname; 6365 } 6366 else 6367 { 6368 size_t indx; 6369 6370 name = lbasename (output_bfd->filename); 6371 def.vd_hash = bfd_elf_hash (name); 6372 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6373 name, FALSE); 6374 if (indx == (size_t) -1) 6375 return FALSE; 6376 defaux.vda_name = indx; 6377 } 6378 defaux.vda_next = 0; 6379 6380 _bfd_elf_swap_verdef_out (output_bfd, &def, 6381 (Elf_External_Verdef *) p); 6382 p += sizeof (Elf_External_Verdef); 6383 if (info->create_default_symver) 6384 { 6385 /* Add a symbol representing this version. */ 6386 bh = NULL; 6387 if (! (_bfd_generic_link_add_one_symbol 6388 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, 6389 0, NULL, FALSE, 6390 get_elf_backend_data (dynobj)->collect, &bh))) 6391 return FALSE; 6392 h = (struct elf_link_hash_entry *) bh; 6393 h->non_elf = 0; 6394 h->def_regular = 1; 6395 h->type = STT_OBJECT; 6396 h->verinfo.vertree = NULL; 6397 6398 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 6399 return FALSE; 6400 6401 /* Create a duplicate of the base version with the same 6402 aux block, but different flags. */ 6403 def.vd_flags = 0; 6404 def.vd_ndx = 2; 6405 def.vd_aux = sizeof (Elf_External_Verdef); 6406 if (verdefs) 6407 def.vd_next = (sizeof (Elf_External_Verdef) 6408 + sizeof (Elf_External_Verdaux)); 6409 else 6410 def.vd_next = 0; 6411 _bfd_elf_swap_verdef_out (output_bfd, &def, 6412 (Elf_External_Verdef *) p); 6413 p += sizeof (Elf_External_Verdef); 6414 } 6415 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 6416 (Elf_External_Verdaux *) p); 6417 p += sizeof (Elf_External_Verdaux); 6418 6419 for (t = verdefs; t != NULL; t = t->next) 6420 { 6421 unsigned int cdeps; 6422 struct bfd_elf_version_deps *n; 6423 6424 /* Don't emit the base version twice. */ 6425 if (t->vernum == 0) 6426 continue; 6427 6428 cdeps = 0; 6429 for (n = t->deps; n != NULL; n = n->next) 6430 ++cdeps; 6431 6432 /* Add a symbol representing this version. */ 6433 bh = NULL; 6434 if (! (_bfd_generic_link_add_one_symbol 6435 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, 6436 0, NULL, FALSE, 6437 get_elf_backend_data (dynobj)->collect, &bh))) 6438 return FALSE; 6439 h = (struct elf_link_hash_entry *) bh; 6440 h->non_elf = 0; 6441 h->def_regular = 1; 6442 h->type = STT_OBJECT; 6443 h->verinfo.vertree = t; 6444 6445 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 6446 return FALSE; 6447 6448 def.vd_version = VER_DEF_CURRENT; 6449 def.vd_flags = 0; 6450 if (t->globals.list == NULL 6451 && t->locals.list == NULL 6452 && ! t->used) 6453 def.vd_flags |= VER_FLG_WEAK; 6454 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); 6455 def.vd_cnt = cdeps + 1; 6456 def.vd_hash = bfd_elf_hash (t->name); 6457 def.vd_aux = sizeof (Elf_External_Verdef); 6458 def.vd_next = 0; 6459 6460 /* If a basever node is next, it *must* be the last node in 6461 the chain, otherwise Verdef construction breaks. */ 6462 if (t->next != NULL && t->next->vernum == 0) 6463 BFD_ASSERT (t->next->next == NULL); 6464 6465 if (t->next != NULL && t->next->vernum != 0) 6466 def.vd_next = (sizeof (Elf_External_Verdef) 6467 + (cdeps + 1) * sizeof (Elf_External_Verdaux)); 6468 6469 _bfd_elf_swap_verdef_out (output_bfd, &def, 6470 (Elf_External_Verdef *) p); 6471 p += sizeof (Elf_External_Verdef); 6472 6473 defaux.vda_name = h->dynstr_index; 6474 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 6475 h->dynstr_index); 6476 defaux.vda_next = 0; 6477 if (t->deps != NULL) 6478 defaux.vda_next = sizeof (Elf_External_Verdaux); 6479 t->name_indx = defaux.vda_name; 6480 6481 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 6482 (Elf_External_Verdaux *) p); 6483 p += sizeof (Elf_External_Verdaux); 6484 6485 for (n = t->deps; n != NULL; n = n->next) 6486 { 6487 if (n->version_needed == NULL) 6488 { 6489 /* This can happen if there was an error in the 6490 version script. */ 6491 defaux.vda_name = 0; 6492 } 6493 else 6494 { 6495 defaux.vda_name = n->version_needed->name_indx; 6496 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 6497 defaux.vda_name); 6498 } 6499 if (n->next == NULL) 6500 defaux.vda_next = 0; 6501 else 6502 defaux.vda_next = sizeof (Elf_External_Verdaux); 6503 6504 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 6505 (Elf_External_Verdaux *) p); 6506 p += sizeof (Elf_External_Verdaux); 6507 } 6508 } 6509 6510 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) 6511 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) 6512 return FALSE; 6513 6514 elf_tdata (output_bfd)->cverdefs = cdefs; 6515 } 6516 6517 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) 6518 { 6519 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) 6520 return FALSE; 6521 } 6522 else if (info->flags & DF_BIND_NOW) 6523 { 6524 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) 6525 return FALSE; 6526 } 6527 6528 if (info->flags_1) 6529 { 6530 if (bfd_link_executable (info)) 6531 info->flags_1 &= ~ (DF_1_INITFIRST 6532 | DF_1_NODELETE 6533 | DF_1_NOOPEN); 6534 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) 6535 return FALSE; 6536 } 6537 6538 /* Work out the size of the version reference section. */ 6539 6540 s = bfd_get_linker_section (dynobj, ".gnu.version_r"); 6541 BFD_ASSERT (s != NULL); 6542 { 6543 struct elf_find_verdep_info sinfo; 6544 6545 sinfo.info = info; 6546 sinfo.vers = elf_tdata (output_bfd)->cverdefs; 6547 if (sinfo.vers == 0) 6548 sinfo.vers = 1; 6549 sinfo.failed = FALSE; 6550 6551 elf_link_hash_traverse (elf_hash_table (info), 6552 _bfd_elf_link_find_version_dependencies, 6553 &sinfo); 6554 if (sinfo.failed) 6555 return FALSE; 6556 6557 if (elf_tdata (output_bfd)->verref == NULL) 6558 s->flags |= SEC_EXCLUDE; 6559 else 6560 { 6561 Elf_Internal_Verneed *t; 6562 unsigned int size; 6563 unsigned int crefs; 6564 bfd_byte *p; 6565 6566 /* Build the version dependency section. */ 6567 size = 0; 6568 crefs = 0; 6569 for (t = elf_tdata (output_bfd)->verref; 6570 t != NULL; 6571 t = t->vn_nextref) 6572 { 6573 Elf_Internal_Vernaux *a; 6574 6575 size += sizeof (Elf_External_Verneed); 6576 ++crefs; 6577 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 6578 size += sizeof (Elf_External_Vernaux); 6579 } 6580 6581 s->size = size; 6582 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 6583 if (s->contents == NULL) 6584 return FALSE; 6585 6586 p = s->contents; 6587 for (t = elf_tdata (output_bfd)->verref; 6588 t != NULL; 6589 t = t->vn_nextref) 6590 { 6591 unsigned int caux; 6592 Elf_Internal_Vernaux *a; 6593 size_t indx; 6594 6595 caux = 0; 6596 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 6597 ++caux; 6598 6599 t->vn_version = VER_NEED_CURRENT; 6600 t->vn_cnt = caux; 6601 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6602 elf_dt_name (t->vn_bfd) != NULL 6603 ? elf_dt_name (t->vn_bfd) 6604 : lbasename (t->vn_bfd->filename), 6605 FALSE); 6606 if (indx == (size_t) -1) 6607 return FALSE; 6608 t->vn_file = indx; 6609 t->vn_aux = sizeof (Elf_External_Verneed); 6610 if (t->vn_nextref == NULL) 6611 t->vn_next = 0; 6612 else 6613 t->vn_next = (sizeof (Elf_External_Verneed) 6614 + caux * sizeof (Elf_External_Vernaux)); 6615 6616 _bfd_elf_swap_verneed_out (output_bfd, t, 6617 (Elf_External_Verneed *) p); 6618 p += sizeof (Elf_External_Verneed); 6619 6620 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 6621 { 6622 a->vna_hash = bfd_elf_hash (a->vna_nodename); 6623 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6624 a->vna_nodename, FALSE); 6625 if (indx == (size_t) -1) 6626 return FALSE; 6627 a->vna_name = indx; 6628 if (a->vna_nextptr == NULL) 6629 a->vna_next = 0; 6630 else 6631 a->vna_next = sizeof (Elf_External_Vernaux); 6632 6633 _bfd_elf_swap_vernaux_out (output_bfd, a, 6634 (Elf_External_Vernaux *) p); 6635 p += sizeof (Elf_External_Vernaux); 6636 } 6637 } 6638 6639 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) 6640 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) 6641 return FALSE; 6642 6643 elf_tdata (output_bfd)->cverrefs = crefs; 6644 } 6645 } 6646 6647 if ((elf_tdata (output_bfd)->cverrefs == 0 6648 && elf_tdata (output_bfd)->cverdefs == 0) 6649 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, 6650 §ion_sym_count) == 0) 6651 { 6652 s = bfd_get_linker_section (dynobj, ".gnu.version"); 6653 s->flags |= SEC_EXCLUDE; 6654 } 6655 } 6656 return TRUE; 6657} 6658 6659/* Find the first non-excluded output section. We'll use its 6660 section symbol for some emitted relocs. */ 6661void 6662_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) 6663{ 6664 asection *s; 6665 6666 for (s = output_bfd->sections; s != NULL; s = s->next) 6667 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC 6668 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) 6669 { 6670 elf_hash_table (info)->text_index_section = s; 6671 break; 6672 } 6673} 6674 6675/* Find two non-excluded output sections, one for code, one for data. 6676 We'll use their section symbols for some emitted relocs. */ 6677void 6678_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) 6679{ 6680 asection *s; 6681 6682 /* Data first, since setting text_index_section changes 6683 _bfd_elf_link_omit_section_dynsym. */ 6684 for (s = output_bfd->sections; s != NULL; s = s->next) 6685 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) 6686 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) 6687 { 6688 elf_hash_table (info)->data_index_section = s; 6689 break; 6690 } 6691 6692 for (s = output_bfd->sections; s != NULL; s = s->next) 6693 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) 6694 == (SEC_ALLOC | SEC_READONLY)) 6695 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) 6696 { 6697 elf_hash_table (info)->text_index_section = s; 6698 break; 6699 } 6700 6701 if (elf_hash_table (info)->text_index_section == NULL) 6702 elf_hash_table (info)->text_index_section 6703 = elf_hash_table (info)->data_index_section; 6704} 6705 6706bfd_boolean 6707bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) 6708{ 6709 const struct elf_backend_data *bed; 6710 unsigned long section_sym_count; 6711 bfd_size_type dynsymcount; 6712 6713 if (!is_elf_hash_table (info->hash)) 6714 return TRUE; 6715 6716 bed = get_elf_backend_data (output_bfd); 6717 (*bed->elf_backend_init_index_section) (output_bfd, info); 6718 6719 /* Assign dynsym indices. In a shared library we generate a section 6720 symbol for each output section, which come first. Next come all 6721 of the back-end allocated local dynamic syms, followed by the rest 6722 of the global symbols. 6723 6724 This is usually not needed for static binaries, however backends 6725 can request to always do it, e.g. the MIPS backend uses dynamic 6726 symbol counts to lay out GOT, which will be produced in the 6727 presence of GOT relocations even in static binaries (holding fixed 6728 data in that case, to satisfy those relocations). */ 6729 6730 if (elf_hash_table (info)->dynamic_sections_created 6731 || bed->always_renumber_dynsyms) 6732 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, 6733 §ion_sym_count); 6734 6735 if (elf_hash_table (info)->dynamic_sections_created) 6736 { 6737 bfd *dynobj; 6738 asection *s; 6739 unsigned int dtagcount; 6740 6741 dynobj = elf_hash_table (info)->dynobj; 6742 6743 /* Work out the size of the symbol version section. */ 6744 s = bfd_get_linker_section (dynobj, ".gnu.version"); 6745 BFD_ASSERT (s != NULL); 6746 if ((s->flags & SEC_EXCLUDE) == 0) 6747 { 6748 s->size = dynsymcount * sizeof (Elf_External_Versym); 6749 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 6750 if (s->contents == NULL) 6751 return FALSE; 6752 6753 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) 6754 return FALSE; 6755 } 6756 6757 /* Set the size of the .dynsym and .hash sections. We counted 6758 the number of dynamic symbols in elf_link_add_object_symbols. 6759 We will build the contents of .dynsym and .hash when we build 6760 the final symbol table, because until then we do not know the 6761 correct value to give the symbols. We built the .dynstr 6762 section as we went along in elf_link_add_object_symbols. */ 6763 s = elf_hash_table (info)->dynsym; 6764 BFD_ASSERT (s != NULL); 6765 s->size = dynsymcount * bed->s->sizeof_sym; 6766 6767 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 6768 if (s->contents == NULL) 6769 return FALSE; 6770 6771 /* The first entry in .dynsym is a dummy symbol. Clear all the 6772 section syms, in case we don't output them all. */ 6773 ++section_sym_count; 6774 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); 6775 6776 elf_hash_table (info)->bucketcount = 0; 6777 6778 /* Compute the size of the hashing table. As a side effect this 6779 computes the hash values for all the names we export. */ 6780 if (info->emit_hash) 6781 { 6782 unsigned long int *hashcodes; 6783 struct hash_codes_info hashinf; 6784 bfd_size_type amt; 6785 unsigned long int nsyms; 6786 size_t bucketcount; 6787 size_t hash_entry_size; 6788 6789 /* Compute the hash values for all exported symbols. At the same 6790 time store the values in an array so that we could use them for 6791 optimizations. */ 6792 amt = dynsymcount * sizeof (unsigned long int); 6793 hashcodes = (unsigned long int *) bfd_malloc (amt); 6794 if (hashcodes == NULL) 6795 return FALSE; 6796 hashinf.hashcodes = hashcodes; 6797 hashinf.error = FALSE; 6798 6799 /* Put all hash values in HASHCODES. */ 6800 elf_link_hash_traverse (elf_hash_table (info), 6801 elf_collect_hash_codes, &hashinf); 6802 if (hashinf.error) 6803 { 6804 free (hashcodes); 6805 return FALSE; 6806 } 6807 6808 nsyms = hashinf.hashcodes - hashcodes; 6809 bucketcount 6810 = compute_bucket_count (info, hashcodes, nsyms, 0); 6811 free (hashcodes); 6812 6813 if (bucketcount == 0) 6814 return FALSE; 6815 6816 elf_hash_table (info)->bucketcount = bucketcount; 6817 6818 s = bfd_get_linker_section (dynobj, ".hash"); 6819 BFD_ASSERT (s != NULL); 6820 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; 6821 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); 6822 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 6823 if (s->contents == NULL) 6824 return FALSE; 6825 6826 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); 6827 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, 6828 s->contents + hash_entry_size); 6829 } 6830 6831 if (info->emit_gnu_hash) 6832 { 6833 size_t i, cnt; 6834 unsigned char *contents; 6835 struct collect_gnu_hash_codes cinfo; 6836 bfd_size_type amt; 6837 size_t bucketcount; 6838 6839 memset (&cinfo, 0, sizeof (cinfo)); 6840 6841 /* Compute the hash values for all exported symbols. At the same 6842 time store the values in an array so that we could use them for 6843 optimizations. */ 6844 amt = dynsymcount * 2 * sizeof (unsigned long int); 6845 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); 6846 if (cinfo.hashcodes == NULL) 6847 return FALSE; 6848 6849 cinfo.hashval = cinfo.hashcodes + dynsymcount; 6850 cinfo.min_dynindx = -1; 6851 cinfo.output_bfd = output_bfd; 6852 cinfo.bed = bed; 6853 6854 /* Put all hash values in HASHCODES. */ 6855 elf_link_hash_traverse (elf_hash_table (info), 6856 elf_collect_gnu_hash_codes, &cinfo); 6857 if (cinfo.error) 6858 { 6859 free (cinfo.hashcodes); 6860 return FALSE; 6861 } 6862 6863 bucketcount 6864 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); 6865 6866 if (bucketcount == 0) 6867 { 6868 free (cinfo.hashcodes); 6869 return FALSE; 6870 } 6871 6872 s = bfd_get_linker_section (dynobj, ".gnu.hash"); 6873 BFD_ASSERT (s != NULL); 6874 6875 if (cinfo.nsyms == 0) 6876 { 6877 /* Empty .gnu.hash section is special. */ 6878 BFD_ASSERT (cinfo.min_dynindx == -1); 6879 free (cinfo.hashcodes); 6880 s->size = 5 * 4 + bed->s->arch_size / 8; 6881 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 6882 if (contents == NULL) 6883 return FALSE; 6884 s->contents = contents; 6885 /* 1 empty bucket. */ 6886 bfd_put_32 (output_bfd, 1, contents); 6887 /* SYMIDX above the special symbol 0. */ 6888 bfd_put_32 (output_bfd, 1, contents + 4); 6889 /* Just one word for bitmask. */ 6890 bfd_put_32 (output_bfd, 1, contents + 8); 6891 /* Only hash fn bloom filter. */ 6892 bfd_put_32 (output_bfd, 0, contents + 12); 6893 /* No hashes are valid - empty bitmask. */ 6894 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); 6895 /* No hashes in the only bucket. */ 6896 bfd_put_32 (output_bfd, 0, 6897 contents + 16 + bed->s->arch_size / 8); 6898 } 6899 else 6900 { 6901 unsigned long int maskwords, maskbitslog2, x; 6902 BFD_ASSERT (cinfo.min_dynindx != -1); 6903 6904 x = cinfo.nsyms; 6905 maskbitslog2 = 1; 6906 while ((x >>= 1) != 0) 6907 ++maskbitslog2; 6908 if (maskbitslog2 < 3) 6909 maskbitslog2 = 5; 6910 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) 6911 maskbitslog2 = maskbitslog2 + 3; 6912 else 6913 maskbitslog2 = maskbitslog2 + 2; 6914 if (bed->s->arch_size == 64) 6915 { 6916 if (maskbitslog2 == 5) 6917 maskbitslog2 = 6; 6918 cinfo.shift1 = 6; 6919 } 6920 else 6921 cinfo.shift1 = 5; 6922 cinfo.mask = (1 << cinfo.shift1) - 1; 6923 cinfo.shift2 = maskbitslog2; 6924 cinfo.maskbits = 1 << maskbitslog2; 6925 maskwords = 1 << (maskbitslog2 - cinfo.shift1); 6926 amt = bucketcount * sizeof (unsigned long int) * 2; 6927 amt += maskwords * sizeof (bfd_vma); 6928 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); 6929 if (cinfo.bitmask == NULL) 6930 { 6931 free (cinfo.hashcodes); 6932 return FALSE; 6933 } 6934 6935 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); 6936 cinfo.indx = cinfo.counts + bucketcount; 6937 cinfo.symindx = dynsymcount - cinfo.nsyms; 6938 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); 6939 6940 /* Determine how often each hash bucket is used. */ 6941 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); 6942 for (i = 0; i < cinfo.nsyms; ++i) 6943 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; 6944 6945 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) 6946 if (cinfo.counts[i] != 0) 6947 { 6948 cinfo.indx[i] = cnt; 6949 cnt += cinfo.counts[i]; 6950 } 6951 BFD_ASSERT (cnt == dynsymcount); 6952 cinfo.bucketcount = bucketcount; 6953 cinfo.local_indx = cinfo.min_dynindx; 6954 6955 s->size = (4 + bucketcount + cinfo.nsyms) * 4; 6956 s->size += cinfo.maskbits / 8; 6957 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 6958 if (contents == NULL) 6959 { 6960 free (cinfo.bitmask); 6961 free (cinfo.hashcodes); 6962 return FALSE; 6963 } 6964 6965 s->contents = contents; 6966 bfd_put_32 (output_bfd, bucketcount, contents); 6967 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); 6968 bfd_put_32 (output_bfd, maskwords, contents + 8); 6969 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); 6970 contents += 16 + cinfo.maskbits / 8; 6971 6972 for (i = 0; i < bucketcount; ++i) 6973 { 6974 if (cinfo.counts[i] == 0) 6975 bfd_put_32 (output_bfd, 0, contents); 6976 else 6977 bfd_put_32 (output_bfd, cinfo.indx[i], contents); 6978 contents += 4; 6979 } 6980 6981 cinfo.contents = contents; 6982 6983 /* Renumber dynamic symbols, populate .gnu.hash section. */ 6984 elf_link_hash_traverse (elf_hash_table (info), 6985 elf_renumber_gnu_hash_syms, &cinfo); 6986 6987 contents = s->contents + 16; 6988 for (i = 0; i < maskwords; ++i) 6989 { 6990 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], 6991 contents); 6992 contents += bed->s->arch_size / 8; 6993 } 6994 6995 free (cinfo.bitmask); 6996 free (cinfo.hashcodes); 6997 } 6998 } 6999 7000 s = bfd_get_linker_section (dynobj, ".dynstr"); 7001 BFD_ASSERT (s != NULL); 7002 7003 elf_finalize_dynstr (output_bfd, info); 7004 7005 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); 7006 7007 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) 7008 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) 7009 return FALSE; 7010 } 7011 7012 return TRUE; 7013} 7014 7015/* Make sure sec_info_type is cleared if sec_info is cleared too. */ 7016 7017static void 7018merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, 7019 asection *sec) 7020{ 7021 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); 7022 sec->sec_info_type = SEC_INFO_TYPE_NONE; 7023} 7024 7025/* Finish SHF_MERGE section merging. */ 7026 7027bfd_boolean 7028_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) 7029{ 7030 bfd *ibfd; 7031 asection *sec; 7032 7033 if (!is_elf_hash_table (info->hash)) 7034 return FALSE; 7035 7036 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 7037 if ((ibfd->flags & DYNAMIC) == 0 7038 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour 7039 && (elf_elfheader (ibfd)->e_ident[EI_CLASS] 7040 == get_elf_backend_data (obfd)->s->elfclass)) 7041 for (sec = ibfd->sections; sec != NULL; sec = sec->next) 7042 if ((sec->flags & SEC_MERGE) != 0 7043 && !bfd_is_abs_section (sec->output_section)) 7044 { 7045 struct bfd_elf_section_data *secdata; 7046 7047 secdata = elf_section_data (sec); 7048 if (! _bfd_add_merge_section (obfd, 7049 &elf_hash_table (info)->merge_info, 7050 sec, &secdata->sec_info)) 7051 return FALSE; 7052 else if (secdata->sec_info) 7053 sec->sec_info_type = SEC_INFO_TYPE_MERGE; 7054 } 7055 7056 if (elf_hash_table (info)->merge_info != NULL) 7057 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, 7058 merge_sections_remove_hook); 7059 return TRUE; 7060} 7061 7062/* Create an entry in an ELF linker hash table. */ 7063 7064struct bfd_hash_entry * 7065_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, 7066 struct bfd_hash_table *table, 7067 const char *string) 7068{ 7069 /* Allocate the structure if it has not already been allocated by a 7070 subclass. */ 7071 if (entry == NULL) 7072 { 7073 entry = (struct bfd_hash_entry *) 7074 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); 7075 if (entry == NULL) 7076 return entry; 7077 } 7078 7079 /* Call the allocation method of the superclass. */ 7080 entry = _bfd_link_hash_newfunc (entry, table, string); 7081 if (entry != NULL) 7082 { 7083 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; 7084 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; 7085 7086 /* Set local fields. */ 7087 ret->indx = -1; 7088 ret->dynindx = -1; 7089 ret->got = htab->init_got_refcount; 7090 ret->plt = htab->init_plt_refcount; 7091 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) 7092 - offsetof (struct elf_link_hash_entry, size))); 7093 /* Assume that we have been called by a non-ELF symbol reader. 7094 This flag is then reset by the code which reads an ELF input 7095 file. This ensures that a symbol created by a non-ELF symbol 7096 reader will have the flag set correctly. */ 7097 ret->non_elf = 1; 7098 } 7099 7100 return entry; 7101} 7102 7103/* Copy data from an indirect symbol to its direct symbol, hiding the 7104 old indirect symbol. Also used for copying flags to a weakdef. */ 7105 7106void 7107_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, 7108 struct elf_link_hash_entry *dir, 7109 struct elf_link_hash_entry *ind) 7110{ 7111 struct elf_link_hash_table *htab; 7112 7113 /* Copy down any references that we may have already seen to the 7114 symbol which just became indirect. */ 7115 7116 if (dir->versioned != versioned_hidden) 7117 dir->ref_dynamic |= ind->ref_dynamic; 7118 dir->ref_regular |= ind->ref_regular; 7119 dir->ref_regular_nonweak |= ind->ref_regular_nonweak; 7120 dir->non_got_ref |= ind->non_got_ref; 7121 dir->needs_plt |= ind->needs_plt; 7122 dir->pointer_equality_needed |= ind->pointer_equality_needed; 7123 7124 if (ind->root.type != bfd_link_hash_indirect) 7125 return; 7126 7127 /* Copy over the global and procedure linkage table refcount entries. 7128 These may have been already set up by a check_relocs routine. */ 7129 htab = elf_hash_table (info); 7130 if (ind->got.refcount > htab->init_got_refcount.refcount) 7131 { 7132 if (dir->got.refcount < 0) 7133 dir->got.refcount = 0; 7134 dir->got.refcount += ind->got.refcount; 7135 ind->got.refcount = htab->init_got_refcount.refcount; 7136 } 7137 7138 if (ind->plt.refcount > htab->init_plt_refcount.refcount) 7139 { 7140 if (dir->plt.refcount < 0) 7141 dir->plt.refcount = 0; 7142 dir->plt.refcount += ind->plt.refcount; 7143 ind->plt.refcount = htab->init_plt_refcount.refcount; 7144 } 7145 7146 if (ind->dynindx != -1) 7147 { 7148 if (dir->dynindx != -1) 7149 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); 7150 dir->dynindx = ind->dynindx; 7151 dir->dynstr_index = ind->dynstr_index; 7152 ind->dynindx = -1; 7153 ind->dynstr_index = 0; 7154 } 7155} 7156 7157void 7158_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, 7159 struct elf_link_hash_entry *h, 7160 bfd_boolean force_local) 7161{ 7162 /* STT_GNU_IFUNC symbol must go through PLT. */ 7163 if (h->type != STT_GNU_IFUNC) 7164 { 7165 h->plt = elf_hash_table (info)->init_plt_offset; 7166 h->needs_plt = 0; 7167 } 7168 if (force_local) 7169 { 7170 h->forced_local = 1; 7171 if (h->dynindx != -1) 7172 { 7173 h->dynindx = -1; 7174 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, 7175 h->dynstr_index); 7176 } 7177 } 7178} 7179 7180/* Initialize an ELF linker hash table. *TABLE has been zeroed by our 7181 caller. */ 7182 7183bfd_boolean 7184_bfd_elf_link_hash_table_init 7185 (struct elf_link_hash_table *table, 7186 bfd *abfd, 7187 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, 7188 struct bfd_hash_table *, 7189 const char *), 7190 unsigned int entsize, 7191 enum elf_target_id target_id) 7192{ 7193 bfd_boolean ret; 7194 int can_refcount = get_elf_backend_data (abfd)->can_refcount; 7195 7196 table->init_got_refcount.refcount = can_refcount - 1; 7197 table->init_plt_refcount.refcount = can_refcount - 1; 7198 table->init_got_offset.offset = -(bfd_vma) 1; 7199 table->init_plt_offset.offset = -(bfd_vma) 1; 7200 /* The first dynamic symbol is a dummy. */ 7201 table->dynsymcount = 1; 7202 7203 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); 7204 7205 table->root.type = bfd_link_elf_hash_table; 7206 table->hash_table_id = target_id; 7207 7208 return ret; 7209} 7210 7211/* Create an ELF linker hash table. */ 7212 7213struct bfd_link_hash_table * 7214_bfd_elf_link_hash_table_create (bfd *abfd) 7215{ 7216 struct elf_link_hash_table *ret; 7217 bfd_size_type amt = sizeof (struct elf_link_hash_table); 7218 7219 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); 7220 if (ret == NULL) 7221 return NULL; 7222 7223 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, 7224 sizeof (struct elf_link_hash_entry), 7225 GENERIC_ELF_DATA)) 7226 { 7227 free (ret); 7228 return NULL; 7229 } 7230 ret->root.hash_table_free = _bfd_elf_link_hash_table_free; 7231 7232 return &ret->root; 7233} 7234 7235/* Destroy an ELF linker hash table. */ 7236 7237void 7238_bfd_elf_link_hash_table_free (bfd *obfd) 7239{ 7240 struct elf_link_hash_table *htab; 7241 7242 htab = (struct elf_link_hash_table *) obfd->link.hash; 7243 if (htab->dynstr != NULL) 7244 _bfd_elf_strtab_free (htab->dynstr); 7245 _bfd_merge_sections_free (htab->merge_info); 7246 _bfd_generic_link_hash_table_free (obfd); 7247} 7248 7249/* This is a hook for the ELF emulation code in the generic linker to 7250 tell the backend linker what file name to use for the DT_NEEDED 7251 entry for a dynamic object. */ 7252 7253void 7254bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) 7255{ 7256 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 7257 && bfd_get_format (abfd) == bfd_object) 7258 elf_dt_name (abfd) = name; 7259} 7260 7261int 7262bfd_elf_get_dyn_lib_class (bfd *abfd) 7263{ 7264 int lib_class; 7265 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 7266 && bfd_get_format (abfd) == bfd_object) 7267 lib_class = elf_dyn_lib_class (abfd); 7268 else 7269 lib_class = 0; 7270 return lib_class; 7271} 7272 7273void 7274bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) 7275{ 7276 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 7277 && bfd_get_format (abfd) == bfd_object) 7278 elf_dyn_lib_class (abfd) = lib_class; 7279} 7280 7281/* Get the list of DT_NEEDED entries for a link. This is a hook for 7282 the linker ELF emulation code. */ 7283 7284struct bfd_link_needed_list * 7285bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, 7286 struct bfd_link_info *info) 7287{ 7288 if (! is_elf_hash_table (info->hash)) 7289 return NULL; 7290 return elf_hash_table (info)->needed; 7291} 7292 7293/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a 7294 hook for the linker ELF emulation code. */ 7295 7296struct bfd_link_needed_list * 7297bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, 7298 struct bfd_link_info *info) 7299{ 7300 if (! is_elf_hash_table (info->hash)) 7301 return NULL; 7302 return elf_hash_table (info)->runpath; 7303} 7304 7305/* Get the name actually used for a dynamic object for a link. This 7306 is the SONAME entry if there is one. Otherwise, it is the string 7307 passed to bfd_elf_set_dt_needed_name, or it is the filename. */ 7308 7309const char * 7310bfd_elf_get_dt_soname (bfd *abfd) 7311{ 7312 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 7313 && bfd_get_format (abfd) == bfd_object) 7314 return elf_dt_name (abfd); 7315 return NULL; 7316} 7317 7318/* Get the list of DT_NEEDED entries from a BFD. This is a hook for 7319 the ELF linker emulation code. */ 7320 7321bfd_boolean 7322bfd_elf_get_bfd_needed_list (bfd *abfd, 7323 struct bfd_link_needed_list **pneeded) 7324{ 7325 asection *s; 7326 bfd_byte *dynbuf = NULL; 7327 unsigned int elfsec; 7328 unsigned long shlink; 7329 bfd_byte *extdyn, *extdynend; 7330 size_t extdynsize; 7331 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 7332 7333 *pneeded = NULL; 7334 7335 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour 7336 || bfd_get_format (abfd) != bfd_object) 7337 return TRUE; 7338 7339 s = bfd_get_section_by_name (abfd, ".dynamic"); 7340 if (s == NULL || s->size == 0) 7341 return TRUE; 7342 7343 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) 7344 goto error_return; 7345 7346 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 7347 if (elfsec == SHN_BAD) 7348 goto error_return; 7349 7350 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 7351 7352 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 7353 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 7354 7355 extdyn = dynbuf; 7356 extdynend = extdyn + s->size; 7357 for (; extdyn < extdynend; extdyn += extdynsize) 7358 { 7359 Elf_Internal_Dyn dyn; 7360 7361 (*swap_dyn_in) (abfd, extdyn, &dyn); 7362 7363 if (dyn.d_tag == DT_NULL) 7364 break; 7365 7366 if (dyn.d_tag == DT_NEEDED) 7367 { 7368 const char *string; 7369 struct bfd_link_needed_list *l; 7370 unsigned int tagv = dyn.d_un.d_val; 7371 bfd_size_type amt; 7372 7373 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 7374 if (string == NULL) 7375 goto error_return; 7376 7377 amt = sizeof *l; 7378 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 7379 if (l == NULL) 7380 goto error_return; 7381 7382 l->by = abfd; 7383 l->name = string; 7384 l->next = *pneeded; 7385 *pneeded = l; 7386 } 7387 } 7388 7389 free (dynbuf); 7390 7391 return TRUE; 7392 7393 error_return: 7394 if (dynbuf != NULL) 7395 free (dynbuf); 7396 return FALSE; 7397} 7398 7399struct elf_symbuf_symbol 7400{ 7401 unsigned long st_name; /* Symbol name, index in string tbl */ 7402 unsigned char st_info; /* Type and binding attributes */ 7403 unsigned char st_other; /* Visibilty, and target specific */ 7404}; 7405 7406struct elf_symbuf_head 7407{ 7408 struct elf_symbuf_symbol *ssym; 7409 size_t count; 7410 unsigned int st_shndx; 7411}; 7412 7413struct elf_symbol 7414{ 7415 union 7416 { 7417 Elf_Internal_Sym *isym; 7418 struct elf_symbuf_symbol *ssym; 7419 } u; 7420 const char *name; 7421}; 7422 7423/* Sort references to symbols by ascending section number. */ 7424 7425static int 7426elf_sort_elf_symbol (const void *arg1, const void *arg2) 7427{ 7428 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; 7429 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; 7430 7431 return s1->st_shndx - s2->st_shndx; 7432} 7433 7434static int 7435elf_sym_name_compare (const void *arg1, const void *arg2) 7436{ 7437 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; 7438 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; 7439 return strcmp (s1->name, s2->name); 7440} 7441 7442static struct elf_symbuf_head * 7443elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) 7444{ 7445 Elf_Internal_Sym **ind, **indbufend, **indbuf; 7446 struct elf_symbuf_symbol *ssym; 7447 struct elf_symbuf_head *ssymbuf, *ssymhead; 7448 size_t i, shndx_count, total_size; 7449 7450 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); 7451 if (indbuf == NULL) 7452 return NULL; 7453 7454 for (ind = indbuf, i = 0; i < symcount; i++) 7455 if (isymbuf[i].st_shndx != SHN_UNDEF) 7456 *ind++ = &isymbuf[i]; 7457 indbufend = ind; 7458 7459 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), 7460 elf_sort_elf_symbol); 7461 7462 shndx_count = 0; 7463 if (indbufend > indbuf) 7464 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) 7465 if (ind[0]->st_shndx != ind[1]->st_shndx) 7466 shndx_count++; 7467 7468 total_size = ((shndx_count + 1) * sizeof (*ssymbuf) 7469 + (indbufend - indbuf) * sizeof (*ssym)); 7470 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); 7471 if (ssymbuf == NULL) 7472 { 7473 free (indbuf); 7474 return NULL; 7475 } 7476 7477 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); 7478 ssymbuf->ssym = NULL; 7479 ssymbuf->count = shndx_count; 7480 ssymbuf->st_shndx = 0; 7481 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) 7482 { 7483 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) 7484 { 7485 ssymhead++; 7486 ssymhead->ssym = ssym; 7487 ssymhead->count = 0; 7488 ssymhead->st_shndx = (*ind)->st_shndx; 7489 } 7490 ssym->st_name = (*ind)->st_name; 7491 ssym->st_info = (*ind)->st_info; 7492 ssym->st_other = (*ind)->st_other; 7493 ssymhead->count++; 7494 } 7495 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count 7496 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) 7497 == total_size)); 7498 7499 free (indbuf); 7500 return ssymbuf; 7501} 7502 7503/* Check if 2 sections define the same set of local and global 7504 symbols. */ 7505 7506static bfd_boolean 7507bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, 7508 struct bfd_link_info *info) 7509{ 7510 bfd *bfd1, *bfd2; 7511 const struct elf_backend_data *bed1, *bed2; 7512 Elf_Internal_Shdr *hdr1, *hdr2; 7513 size_t symcount1, symcount2; 7514 Elf_Internal_Sym *isymbuf1, *isymbuf2; 7515 struct elf_symbuf_head *ssymbuf1, *ssymbuf2; 7516 Elf_Internal_Sym *isym, *isymend; 7517 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; 7518 size_t count1, count2, i; 7519 unsigned int shndx1, shndx2; 7520 bfd_boolean result; 7521 7522 bfd1 = sec1->owner; 7523 bfd2 = sec2->owner; 7524 7525 /* Both sections have to be in ELF. */ 7526 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour 7527 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) 7528 return FALSE; 7529 7530 if (elf_section_type (sec1) != elf_section_type (sec2)) 7531 return FALSE; 7532 7533 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); 7534 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); 7535 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) 7536 return FALSE; 7537 7538 bed1 = get_elf_backend_data (bfd1); 7539 bed2 = get_elf_backend_data (bfd2); 7540 hdr1 = &elf_tdata (bfd1)->symtab_hdr; 7541 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; 7542 hdr2 = &elf_tdata (bfd2)->symtab_hdr; 7543 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; 7544 7545 if (symcount1 == 0 || symcount2 == 0) 7546 return FALSE; 7547 7548 result = FALSE; 7549 isymbuf1 = NULL; 7550 isymbuf2 = NULL; 7551 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; 7552 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; 7553 7554 if (ssymbuf1 == NULL) 7555 { 7556 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, 7557 NULL, NULL, NULL); 7558 if (isymbuf1 == NULL) 7559 goto done; 7560 7561 if (!info->reduce_memory_overheads) 7562 elf_tdata (bfd1)->symbuf = ssymbuf1 7563 = elf_create_symbuf (symcount1, isymbuf1); 7564 } 7565 7566 if (ssymbuf1 == NULL || ssymbuf2 == NULL) 7567 { 7568 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, 7569 NULL, NULL, NULL); 7570 if (isymbuf2 == NULL) 7571 goto done; 7572 7573 if (ssymbuf1 != NULL && !info->reduce_memory_overheads) 7574 elf_tdata (bfd2)->symbuf = ssymbuf2 7575 = elf_create_symbuf (symcount2, isymbuf2); 7576 } 7577 7578 if (ssymbuf1 != NULL && ssymbuf2 != NULL) 7579 { 7580 /* Optimized faster version. */ 7581 size_t lo, hi, mid; 7582 struct elf_symbol *symp; 7583 struct elf_symbuf_symbol *ssym, *ssymend; 7584 7585 lo = 0; 7586 hi = ssymbuf1->count; 7587 ssymbuf1++; 7588 count1 = 0; 7589 while (lo < hi) 7590 { 7591 mid = (lo + hi) / 2; 7592 if (shndx1 < ssymbuf1[mid].st_shndx) 7593 hi = mid; 7594 else if (shndx1 > ssymbuf1[mid].st_shndx) 7595 lo = mid + 1; 7596 else 7597 { 7598 count1 = ssymbuf1[mid].count; 7599 ssymbuf1 += mid; 7600 break; 7601 } 7602 } 7603 7604 lo = 0; 7605 hi = ssymbuf2->count; 7606 ssymbuf2++; 7607 count2 = 0; 7608 while (lo < hi) 7609 { 7610 mid = (lo + hi) / 2; 7611 if (shndx2 < ssymbuf2[mid].st_shndx) 7612 hi = mid; 7613 else if (shndx2 > ssymbuf2[mid].st_shndx) 7614 lo = mid + 1; 7615 else 7616 { 7617 count2 = ssymbuf2[mid].count; 7618 ssymbuf2 += mid; 7619 break; 7620 } 7621 } 7622 7623 if (count1 == 0 || count2 == 0 || count1 != count2) 7624 goto done; 7625 7626 symtable1 7627 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); 7628 symtable2 7629 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); 7630 if (symtable1 == NULL || symtable2 == NULL) 7631 goto done; 7632 7633 symp = symtable1; 7634 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; 7635 ssym < ssymend; ssym++, symp++) 7636 { 7637 symp->u.ssym = ssym; 7638 symp->name = bfd_elf_string_from_elf_section (bfd1, 7639 hdr1->sh_link, 7640 ssym->st_name); 7641 } 7642 7643 symp = symtable2; 7644 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; 7645 ssym < ssymend; ssym++, symp++) 7646 { 7647 symp->u.ssym = ssym; 7648 symp->name = bfd_elf_string_from_elf_section (bfd2, 7649 hdr2->sh_link, 7650 ssym->st_name); 7651 } 7652 7653 /* Sort symbol by name. */ 7654 qsort (symtable1, count1, sizeof (struct elf_symbol), 7655 elf_sym_name_compare); 7656 qsort (symtable2, count1, sizeof (struct elf_symbol), 7657 elf_sym_name_compare); 7658 7659 for (i = 0; i < count1; i++) 7660 /* Two symbols must have the same binding, type and name. */ 7661 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info 7662 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other 7663 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) 7664 goto done; 7665 7666 result = TRUE; 7667 goto done; 7668 } 7669 7670 symtable1 = (struct elf_symbol *) 7671 bfd_malloc (symcount1 * sizeof (struct elf_symbol)); 7672 symtable2 = (struct elf_symbol *) 7673 bfd_malloc (symcount2 * sizeof (struct elf_symbol)); 7674 if (symtable1 == NULL || symtable2 == NULL) 7675 goto done; 7676 7677 /* Count definitions in the section. */ 7678 count1 = 0; 7679 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) 7680 if (isym->st_shndx == shndx1) 7681 symtable1[count1++].u.isym = isym; 7682 7683 count2 = 0; 7684 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) 7685 if (isym->st_shndx == shndx2) 7686 symtable2[count2++].u.isym = isym; 7687 7688 if (count1 == 0 || count2 == 0 || count1 != count2) 7689 goto done; 7690 7691 for (i = 0; i < count1; i++) 7692 symtable1[i].name 7693 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, 7694 symtable1[i].u.isym->st_name); 7695 7696 for (i = 0; i < count2; i++) 7697 symtable2[i].name 7698 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, 7699 symtable2[i].u.isym->st_name); 7700 7701 /* Sort symbol by name. */ 7702 qsort (symtable1, count1, sizeof (struct elf_symbol), 7703 elf_sym_name_compare); 7704 qsort (symtable2, count1, sizeof (struct elf_symbol), 7705 elf_sym_name_compare); 7706 7707 for (i = 0; i < count1; i++) 7708 /* Two symbols must have the same binding, type and name. */ 7709 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info 7710 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other 7711 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) 7712 goto done; 7713 7714 result = TRUE; 7715 7716done: 7717 if (symtable1) 7718 free (symtable1); 7719 if (symtable2) 7720 free (symtable2); 7721 if (isymbuf1) 7722 free (isymbuf1); 7723 if (isymbuf2) 7724 free (isymbuf2); 7725 7726 return result; 7727} 7728 7729/* Return TRUE if 2 section types are compatible. */ 7730 7731bfd_boolean 7732_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, 7733 bfd *bbfd, const asection *bsec) 7734{ 7735 if (asec == NULL 7736 || bsec == NULL 7737 || abfd->xvec->flavour != bfd_target_elf_flavour 7738 || bbfd->xvec->flavour != bfd_target_elf_flavour) 7739 return TRUE; 7740 7741 return elf_section_type (asec) == elf_section_type (bsec); 7742} 7743 7744/* Final phase of ELF linker. */ 7745 7746/* A structure we use to avoid passing large numbers of arguments. */ 7747 7748struct elf_final_link_info 7749{ 7750 /* General link information. */ 7751 struct bfd_link_info *info; 7752 /* Output BFD. */ 7753 bfd *output_bfd; 7754 /* Symbol string table. */ 7755 struct elf_strtab_hash *symstrtab; 7756 /* .hash section. */ 7757 asection *hash_sec; 7758 /* symbol version section (.gnu.version). */ 7759 asection *symver_sec; 7760 /* Buffer large enough to hold contents of any section. */ 7761 bfd_byte *contents; 7762 /* Buffer large enough to hold external relocs of any section. */ 7763 void *external_relocs; 7764 /* Buffer large enough to hold internal relocs of any section. */ 7765 Elf_Internal_Rela *internal_relocs; 7766 /* Buffer large enough to hold external local symbols of any input 7767 BFD. */ 7768 bfd_byte *external_syms; 7769 /* And a buffer for symbol section indices. */ 7770 Elf_External_Sym_Shndx *locsym_shndx; 7771 /* Buffer large enough to hold internal local symbols of any input 7772 BFD. */ 7773 Elf_Internal_Sym *internal_syms; 7774 /* Array large enough to hold a symbol index for each local symbol 7775 of any input BFD. */ 7776 long *indices; 7777 /* Array large enough to hold a section pointer for each local 7778 symbol of any input BFD. */ 7779 asection **sections; 7780 /* Buffer for SHT_SYMTAB_SHNDX section. */ 7781 Elf_External_Sym_Shndx *symshndxbuf; 7782 /* Number of STT_FILE syms seen. */ 7783 size_t filesym_count; 7784}; 7785 7786/* This struct is used to pass information to elf_link_output_extsym. */ 7787 7788struct elf_outext_info 7789{ 7790 bfd_boolean failed; 7791 bfd_boolean localsyms; 7792 bfd_boolean file_sym_done; 7793 struct elf_final_link_info *flinfo; 7794}; 7795 7796 7797/* Support for evaluating a complex relocation. 7798 7799 Complex relocations are generalized, self-describing relocations. The 7800 implementation of them consists of two parts: complex symbols, and the 7801 relocations themselves. 7802 7803 The relocations are use a reserved elf-wide relocation type code (R_RELC 7804 external / BFD_RELOC_RELC internal) and an encoding of relocation field 7805 information (start bit, end bit, word width, etc) into the addend. This 7806 information is extracted from CGEN-generated operand tables within gas. 7807 7808 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC 7809 internal) representing prefix-notation expressions, including but not 7810 limited to those sorts of expressions normally encoded as addends in the 7811 addend field. The symbol mangling format is: 7812 7813 <node> := <literal> 7814 | <unary-operator> ':' <node> 7815 | <binary-operator> ':' <node> ':' <node> 7816 ; 7817 7818 <literal> := 's' <digits=N> ':' <N character symbol name> 7819 | 'S' <digits=N> ':' <N character section name> 7820 | '#' <hexdigits> 7821 ; 7822 7823 <binary-operator> := as in C 7824 <unary-operator> := as in C, plus "0-" for unambiguous negation. */ 7825 7826static void 7827set_symbol_value (bfd *bfd_with_globals, 7828 Elf_Internal_Sym *isymbuf, 7829 size_t locsymcount, 7830 size_t symidx, 7831 bfd_vma val) 7832{ 7833 struct elf_link_hash_entry **sym_hashes; 7834 struct elf_link_hash_entry *h; 7835 size_t extsymoff = locsymcount; 7836 7837 if (symidx < locsymcount) 7838 { 7839 Elf_Internal_Sym *sym; 7840 7841 sym = isymbuf + symidx; 7842 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) 7843 { 7844 /* It is a local symbol: move it to the 7845 "absolute" section and give it a value. */ 7846 sym->st_shndx = SHN_ABS; 7847 sym->st_value = val; 7848 return; 7849 } 7850 BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); 7851 extsymoff = 0; 7852 } 7853 7854 /* It is a global symbol: set its link type 7855 to "defined" and give it a value. */ 7856 7857 sym_hashes = elf_sym_hashes (bfd_with_globals); 7858 h = sym_hashes [symidx - extsymoff]; 7859 while (h->root.type == bfd_link_hash_indirect 7860 || h->root.type == bfd_link_hash_warning) 7861 h = (struct elf_link_hash_entry *) h->root.u.i.link; 7862 h->root.type = bfd_link_hash_defined; 7863 h->root.u.def.value = val; 7864 h->root.u.def.section = bfd_abs_section_ptr; 7865} 7866 7867static bfd_boolean 7868resolve_symbol (const char *name, 7869 bfd *input_bfd, 7870 struct elf_final_link_info *flinfo, 7871 bfd_vma *result, 7872 Elf_Internal_Sym *isymbuf, 7873 size_t locsymcount) 7874{ 7875 Elf_Internal_Sym *sym; 7876 struct bfd_link_hash_entry *global_entry; 7877 const char *candidate = NULL; 7878 Elf_Internal_Shdr *symtab_hdr; 7879 size_t i; 7880 7881 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; 7882 7883 for (i = 0; i < locsymcount; ++ i) 7884 { 7885 sym = isymbuf + i; 7886 7887 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) 7888 continue; 7889 7890 candidate = bfd_elf_string_from_elf_section (input_bfd, 7891 symtab_hdr->sh_link, 7892 sym->st_name); 7893#ifdef DEBUG 7894 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", 7895 name, candidate, (unsigned long) sym->st_value); 7896#endif 7897 if (candidate && strcmp (candidate, name) == 0) 7898 { 7899 asection *sec = flinfo->sections [i]; 7900 7901 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); 7902 *result += sec->output_offset + sec->output_section->vma; 7903#ifdef DEBUG 7904 printf ("Found symbol with value %8.8lx\n", 7905 (unsigned long) *result); 7906#endif 7907 return TRUE; 7908 } 7909 } 7910 7911 /* Hmm, haven't found it yet. perhaps it is a global. */ 7912 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, 7913 FALSE, FALSE, TRUE); 7914 if (!global_entry) 7915 return FALSE; 7916 7917 if (global_entry->type == bfd_link_hash_defined 7918 || global_entry->type == bfd_link_hash_defweak) 7919 { 7920 *result = (global_entry->u.def.value 7921 + global_entry->u.def.section->output_section->vma 7922 + global_entry->u.def.section->output_offset); 7923#ifdef DEBUG 7924 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", 7925 global_entry->root.string, (unsigned long) *result); 7926#endif 7927 return TRUE; 7928 } 7929 7930 return FALSE; 7931} 7932 7933/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in 7934 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section 7935 names like "foo.end" which is the end address of section "foo". */ 7936 7937static bfd_boolean 7938resolve_section (const char *name, 7939 asection *sections, 7940 bfd_vma *result, 7941 bfd * abfd) 7942{ 7943 asection *curr; 7944 unsigned int len; 7945 7946 for (curr = sections; curr; curr = curr->next) 7947 if (strcmp (curr->name, name) == 0) 7948 { 7949 *result = curr->vma; 7950 return TRUE; 7951 } 7952 7953 /* Hmm. still haven't found it. try pseudo-section names. */ 7954 /* FIXME: This could be coded more efficiently... */ 7955 for (curr = sections; curr; curr = curr->next) 7956 { 7957 len = strlen (curr->name); 7958 if (len > strlen (name)) 7959 continue; 7960 7961 if (strncmp (curr->name, name, len) == 0) 7962 { 7963 if (strncmp (".end", name + len, 4) == 0) 7964 { 7965 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); 7966 return TRUE; 7967 } 7968 7969 /* Insert more pseudo-section names here, if you like. */ 7970 } 7971 } 7972 7973 return FALSE; 7974} 7975 7976static void 7977undefined_reference (const char *reftype, const char *name) 7978{ 7979 /* xgettext:c-format */ 7980 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), 7981 reftype, name); 7982} 7983 7984static bfd_boolean 7985eval_symbol (bfd_vma *result, 7986 const char **symp, 7987 bfd *input_bfd, 7988 struct elf_final_link_info *flinfo, 7989 bfd_vma dot, 7990 Elf_Internal_Sym *isymbuf, 7991 size_t locsymcount, 7992 int signed_p) 7993{ 7994 size_t len; 7995 size_t symlen; 7996 bfd_vma a; 7997 bfd_vma b; 7998 char symbuf[4096]; 7999 const char *sym = *symp; 8000 const char *symend; 8001 bfd_boolean symbol_is_section = FALSE; 8002 8003 len = strlen (sym); 8004 symend = sym + len; 8005 8006 if (len < 1 || len > sizeof (symbuf)) 8007 { 8008 bfd_set_error (bfd_error_invalid_operation); 8009 return FALSE; 8010 } 8011 8012 switch (* sym) 8013 { 8014 case '.': 8015 *result = dot; 8016 *symp = sym + 1; 8017 return TRUE; 8018 8019 case '#': 8020 ++sym; 8021 *result = strtoul (sym, (char **) symp, 16); 8022 return TRUE; 8023 8024 case 'S': 8025 symbol_is_section = TRUE; 8026 /* Fall through. */ 8027 case 's': 8028 ++sym; 8029 symlen = strtol (sym, (char **) symp, 10); 8030 sym = *symp + 1; /* Skip the trailing ':'. */ 8031 8032 if (symend < sym || symlen + 1 > sizeof (symbuf)) 8033 { 8034 bfd_set_error (bfd_error_invalid_operation); 8035 return FALSE; 8036 } 8037 8038 memcpy (symbuf, sym, symlen); 8039 symbuf[symlen] = '\0'; 8040 *symp = sym + symlen; 8041 8042 /* Is it always possible, with complex symbols, that gas "mis-guessed" 8043 the symbol as a section, or vice-versa. so we're pretty liberal in our 8044 interpretation here; section means "try section first", not "must be a 8045 section", and likewise with symbol. */ 8046 8047 if (symbol_is_section) 8048 { 8049 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) 8050 && !resolve_symbol (symbuf, input_bfd, flinfo, result, 8051 isymbuf, locsymcount)) 8052 { 8053 undefined_reference ("section", symbuf); 8054 return FALSE; 8055 } 8056 } 8057 else 8058 { 8059 if (!resolve_symbol (symbuf, input_bfd, flinfo, result, 8060 isymbuf, locsymcount) 8061 && !resolve_section (symbuf, flinfo->output_bfd->sections, 8062 result, input_bfd)) 8063 { 8064 undefined_reference ("symbol", symbuf); 8065 return FALSE; 8066 } 8067 } 8068 8069 return TRUE; 8070 8071 /* All that remains are operators. */ 8072 8073#define UNARY_OP(op) \ 8074 if (strncmp (sym, #op, strlen (#op)) == 0) \ 8075 { \ 8076 sym += strlen (#op); \ 8077 if (*sym == ':') \ 8078 ++sym; \ 8079 *symp = sym; \ 8080 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ 8081 isymbuf, locsymcount, signed_p)) \ 8082 return FALSE; \ 8083 if (signed_p) \ 8084 *result = op ((bfd_signed_vma) a); \ 8085 else \ 8086 *result = op a; \ 8087 return TRUE; \ 8088 } 8089 8090#define BINARY_OP(op) \ 8091 if (strncmp (sym, #op, strlen (#op)) == 0) \ 8092 { \ 8093 sym += strlen (#op); \ 8094 if (*sym == ':') \ 8095 ++sym; \ 8096 *symp = sym; \ 8097 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ 8098 isymbuf, locsymcount, signed_p)) \ 8099 return FALSE; \ 8100 ++*symp; \ 8101 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ 8102 isymbuf, locsymcount, signed_p)) \ 8103 return FALSE; \ 8104 if (signed_p) \ 8105 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ 8106 else \ 8107 *result = a op b; \ 8108 return TRUE; \ 8109 } 8110 8111 default: 8112 UNARY_OP (0-); 8113 BINARY_OP (<<); 8114 BINARY_OP (>>); 8115 BINARY_OP (==); 8116 BINARY_OP (!=); 8117 BINARY_OP (<=); 8118 BINARY_OP (>=); 8119 BINARY_OP (&&); 8120 BINARY_OP (||); 8121 UNARY_OP (~); 8122 UNARY_OP (!); 8123 BINARY_OP (*); 8124 BINARY_OP (/); 8125 BINARY_OP (%); 8126 BINARY_OP (^); 8127 BINARY_OP (|); 8128 BINARY_OP (&); 8129 BINARY_OP (+); 8130 BINARY_OP (-); 8131 BINARY_OP (<); 8132 BINARY_OP (>); 8133#undef UNARY_OP 8134#undef BINARY_OP 8135 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); 8136 bfd_set_error (bfd_error_invalid_operation); 8137 return FALSE; 8138 } 8139} 8140 8141static void 8142put_value (bfd_vma size, 8143 unsigned long chunksz, 8144 bfd *input_bfd, 8145 bfd_vma x, 8146 bfd_byte *location) 8147{ 8148 location += (size - chunksz); 8149 8150 for (; size; size -= chunksz, location -= chunksz) 8151 { 8152 switch (chunksz) 8153 { 8154 case 1: 8155 bfd_put_8 (input_bfd, x, location); 8156 x >>= 8; 8157 break; 8158 case 2: 8159 bfd_put_16 (input_bfd, x, location); 8160 x >>= 16; 8161 break; 8162 case 4: 8163 bfd_put_32 (input_bfd, x, location); 8164 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ 8165 x >>= 16; 8166 x >>= 16; 8167 break; 8168#ifdef BFD64 8169 case 8: 8170 bfd_put_64 (input_bfd, x, location); 8171 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ 8172 x >>= 32; 8173 x >>= 32; 8174 break; 8175#endif 8176 default: 8177 abort (); 8178 break; 8179 } 8180 } 8181} 8182 8183static bfd_vma 8184get_value (bfd_vma size, 8185 unsigned long chunksz, 8186 bfd *input_bfd, 8187 bfd_byte *location) 8188{ 8189 int shift; 8190 bfd_vma x = 0; 8191 8192 /* Sanity checks. */ 8193 BFD_ASSERT (chunksz <= sizeof (x) 8194 && size >= chunksz 8195 && chunksz != 0 8196 && (size % chunksz) == 0 8197 && input_bfd != NULL 8198 && location != NULL); 8199 8200 if (chunksz == sizeof (x)) 8201 { 8202 BFD_ASSERT (size == chunksz); 8203 8204 /* Make sure that we do not perform an undefined shift operation. 8205 We know that size == chunksz so there will only be one iteration 8206 of the loop below. */ 8207 shift = 0; 8208 } 8209 else 8210 shift = 8 * chunksz; 8211 8212 for (; size; size -= chunksz, location += chunksz) 8213 { 8214 switch (chunksz) 8215 { 8216 case 1: 8217 x = (x << shift) | bfd_get_8 (input_bfd, location); 8218 break; 8219 case 2: 8220 x = (x << shift) | bfd_get_16 (input_bfd, location); 8221 break; 8222 case 4: 8223 x = (x << shift) | bfd_get_32 (input_bfd, location); 8224 break; 8225#ifdef BFD64 8226 case 8: 8227 x = (x << shift) | bfd_get_64 (input_bfd, location); 8228 break; 8229#endif 8230 default: 8231 abort (); 8232 } 8233 } 8234 return x; 8235} 8236 8237static void 8238decode_complex_addend (unsigned long *start, /* in bits */ 8239 unsigned long *oplen, /* in bits */ 8240 unsigned long *len, /* in bits */ 8241 unsigned long *wordsz, /* in bytes */ 8242 unsigned long *chunksz, /* in bytes */ 8243 unsigned long *lsb0_p, 8244 unsigned long *signed_p, 8245 unsigned long *trunc_p, 8246 unsigned long encoded) 8247{ 8248 * start = encoded & 0x3F; 8249 * len = (encoded >> 6) & 0x3F; 8250 * oplen = (encoded >> 12) & 0x3F; 8251 * wordsz = (encoded >> 18) & 0xF; 8252 * chunksz = (encoded >> 22) & 0xF; 8253 * lsb0_p = (encoded >> 27) & 1; 8254 * signed_p = (encoded >> 28) & 1; 8255 * trunc_p = (encoded >> 29) & 1; 8256} 8257 8258bfd_reloc_status_type 8259bfd_elf_perform_complex_relocation (bfd *input_bfd, 8260 asection *input_section ATTRIBUTE_UNUSED, 8261 bfd_byte *contents, 8262 Elf_Internal_Rela *rel, 8263 bfd_vma relocation) 8264{ 8265 bfd_vma shift, x, mask; 8266 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; 8267 bfd_reloc_status_type r; 8268 8269 /* Perform this reloc, since it is complex. 8270 (this is not to say that it necessarily refers to a complex 8271 symbol; merely that it is a self-describing CGEN based reloc. 8272 i.e. the addend has the complete reloc information (bit start, end, 8273 word size, etc) encoded within it.). */ 8274 8275 decode_complex_addend (&start, &oplen, &len, &wordsz, 8276 &chunksz, &lsb0_p, &signed_p, 8277 &trunc_p, rel->r_addend); 8278 8279 mask = (((1L << (len - 1)) - 1) << 1) | 1; 8280 8281 if (lsb0_p) 8282 shift = (start + 1) - len; 8283 else 8284 shift = (8 * wordsz) - (start + len); 8285 8286 x = get_value (wordsz, chunksz, input_bfd, 8287 contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); 8288 8289#ifdef DEBUG 8290 printf ("Doing complex reloc: " 8291 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " 8292 "chunksz %ld, start %ld, len %ld, oplen %ld\n" 8293 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", 8294 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, 8295 oplen, (unsigned long) x, (unsigned long) mask, 8296 (unsigned long) relocation); 8297#endif 8298 8299 r = bfd_reloc_ok; 8300 if (! trunc_p) 8301 /* Now do an overflow check. */ 8302 r = bfd_check_overflow ((signed_p 8303 ? complain_overflow_signed 8304 : complain_overflow_unsigned), 8305 len, 0, (8 * wordsz), 8306 relocation); 8307 8308 /* Do the deed. */ 8309 x = (x & ~(mask << shift)) | ((relocation & mask) << shift); 8310 8311#ifdef DEBUG 8312 printf (" relocation: %8.8lx\n" 8313 " shifted mask: %8.8lx\n" 8314 " shifted/masked reloc: %8.8lx\n" 8315 " result: %8.8lx\n", 8316 (unsigned long) relocation, (unsigned long) (mask << shift), 8317 (unsigned long) ((relocation & mask) << shift), (unsigned long) x); 8318#endif 8319 put_value (wordsz, chunksz, input_bfd, x, 8320 contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); 8321 return r; 8322} 8323 8324/* Functions to read r_offset from external (target order) reloc 8325 entry. Faster than bfd_getl32 et al, because we let the compiler 8326 know the value is aligned. */ 8327 8328static bfd_vma 8329ext32l_r_offset (const void *p) 8330{ 8331 union aligned32 8332 { 8333 uint32_t v; 8334 unsigned char c[4]; 8335 }; 8336 const union aligned32 *a 8337 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; 8338 8339 uint32_t aval = ( (uint32_t) a->c[0] 8340 | (uint32_t) a->c[1] << 8 8341 | (uint32_t) a->c[2] << 16 8342 | (uint32_t) a->c[3] << 24); 8343 return aval; 8344} 8345 8346static bfd_vma 8347ext32b_r_offset (const void *p) 8348{ 8349 union aligned32 8350 { 8351 uint32_t v; 8352 unsigned char c[4]; 8353 }; 8354 const union aligned32 *a 8355 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; 8356 8357 uint32_t aval = ( (uint32_t) a->c[0] << 24 8358 | (uint32_t) a->c[1] << 16 8359 | (uint32_t) a->c[2] << 8 8360 | (uint32_t) a->c[3]); 8361 return aval; 8362} 8363 8364#ifdef BFD_HOST_64_BIT 8365static bfd_vma 8366ext64l_r_offset (const void *p) 8367{ 8368 union aligned64 8369 { 8370 uint64_t v; 8371 unsigned char c[8]; 8372 }; 8373 const union aligned64 *a 8374 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; 8375 8376 uint64_t aval = ( (uint64_t) a->c[0] 8377 | (uint64_t) a->c[1] << 8 8378 | (uint64_t) a->c[2] << 16 8379 | (uint64_t) a->c[3] << 24 8380 | (uint64_t) a->c[4] << 32 8381 | (uint64_t) a->c[5] << 40 8382 | (uint64_t) a->c[6] << 48 8383 | (uint64_t) a->c[7] << 56); 8384 return aval; 8385} 8386 8387static bfd_vma 8388ext64b_r_offset (const void *p) 8389{ 8390 union aligned64 8391 { 8392 uint64_t v; 8393 unsigned char c[8]; 8394 }; 8395 const union aligned64 *a 8396 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; 8397 8398 uint64_t aval = ( (uint64_t) a->c[0] << 56 8399 | (uint64_t) a->c[1] << 48 8400 | (uint64_t) a->c[2] << 40 8401 | (uint64_t) a->c[3] << 32 8402 | (uint64_t) a->c[4] << 24 8403 | (uint64_t) a->c[5] << 16 8404 | (uint64_t) a->c[6] << 8 8405 | (uint64_t) a->c[7]); 8406 return aval; 8407} 8408#endif 8409 8410/* When performing a relocatable link, the input relocations are 8411 preserved. But, if they reference global symbols, the indices 8412 referenced must be updated. Update all the relocations found in 8413 RELDATA. */ 8414 8415static bfd_boolean 8416elf_link_adjust_relocs (bfd *abfd, 8417 asection *sec, 8418 struct bfd_elf_section_reloc_data *reldata, 8419 bfd_boolean sort) 8420{ 8421 unsigned int i; 8422 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8423 bfd_byte *erela; 8424 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 8425 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 8426 bfd_vma r_type_mask; 8427 int r_sym_shift; 8428 unsigned int count = reldata->count; 8429 struct elf_link_hash_entry **rel_hash = reldata->hashes; 8430 8431 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) 8432 { 8433 swap_in = bed->s->swap_reloc_in; 8434 swap_out = bed->s->swap_reloc_out; 8435 } 8436 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) 8437 { 8438 swap_in = bed->s->swap_reloca_in; 8439 swap_out = bed->s->swap_reloca_out; 8440 } 8441 else 8442 abort (); 8443 8444 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) 8445 abort (); 8446 8447 if (bed->s->arch_size == 32) 8448 { 8449 r_type_mask = 0xff; 8450 r_sym_shift = 8; 8451 } 8452 else 8453 { 8454 r_type_mask = 0xffffffff; 8455 r_sym_shift = 32; 8456 } 8457 8458 erela = reldata->hdr->contents; 8459 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) 8460 { 8461 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; 8462 unsigned int j; 8463 8464 if (*rel_hash == NULL) 8465 continue; 8466 8467 BFD_ASSERT ((*rel_hash)->indx >= 0); 8468 8469 (*swap_in) (abfd, erela, irela); 8470 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) 8471 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift 8472 | (irela[j].r_info & r_type_mask)); 8473 (*swap_out) (abfd, irela, erela); 8474 } 8475 8476 if (bed->elf_backend_update_relocs) 8477 (*bed->elf_backend_update_relocs) (sec, reldata); 8478 8479 if (sort && count != 0) 8480 { 8481 bfd_vma (*ext_r_off) (const void *); 8482 bfd_vma r_off; 8483 size_t elt_size; 8484 bfd_byte *base, *end, *p, *loc; 8485 bfd_byte *buf = NULL; 8486 8487 if (bed->s->arch_size == 32) 8488 { 8489 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) 8490 ext_r_off = ext32l_r_offset; 8491 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) 8492 ext_r_off = ext32b_r_offset; 8493 else 8494 abort (); 8495 } 8496 else 8497 { 8498#ifdef BFD_HOST_64_BIT 8499 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) 8500 ext_r_off = ext64l_r_offset; 8501 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) 8502 ext_r_off = ext64b_r_offset; 8503 else 8504#endif 8505 abort (); 8506 } 8507 8508 /* Must use a stable sort here. A modified insertion sort, 8509 since the relocs are mostly sorted already. */ 8510 elt_size = reldata->hdr->sh_entsize; 8511 base = reldata->hdr->contents; 8512 end = base + count * elt_size; 8513 if (elt_size > sizeof (Elf64_External_Rela)) 8514 abort (); 8515 8516 /* Ensure the first element is lowest. This acts as a sentinel, 8517 speeding the main loop below. */ 8518 r_off = (*ext_r_off) (base); 8519 for (p = loc = base; (p += elt_size) < end; ) 8520 { 8521 bfd_vma r_off2 = (*ext_r_off) (p); 8522 if (r_off > r_off2) 8523 { 8524 r_off = r_off2; 8525 loc = p; 8526 } 8527 } 8528 if (loc != base) 8529 { 8530 /* Don't just swap *base and *loc as that changes the order 8531 of the original base[0] and base[1] if they happen to 8532 have the same r_offset. */ 8533 bfd_byte onebuf[sizeof (Elf64_External_Rela)]; 8534 memcpy (onebuf, loc, elt_size); 8535 memmove (base + elt_size, base, loc - base); 8536 memcpy (base, onebuf, elt_size); 8537 } 8538 8539 for (p = base + elt_size; (p += elt_size) < end; ) 8540 { 8541 /* base to p is sorted, *p is next to insert. */ 8542 r_off = (*ext_r_off) (p); 8543 /* Search the sorted region for location to insert. */ 8544 loc = p - elt_size; 8545 while (r_off < (*ext_r_off) (loc)) 8546 loc -= elt_size; 8547 loc += elt_size; 8548 if (loc != p) 8549 { 8550 /* Chances are there is a run of relocs to insert here, 8551 from one of more input files. Files are not always 8552 linked in order due to the way elf_link_input_bfd is 8553 called. See pr17666. */ 8554 size_t sortlen = p - loc; 8555 bfd_vma r_off2 = (*ext_r_off) (loc); 8556 size_t runlen = elt_size; 8557 size_t buf_size = 96 * 1024; 8558 while (p + runlen < end 8559 && (sortlen <= buf_size 8560 || runlen + elt_size <= buf_size) 8561 && r_off2 > (*ext_r_off) (p + runlen)) 8562 runlen += elt_size; 8563 if (buf == NULL) 8564 { 8565 buf = bfd_malloc (buf_size); 8566 if (buf == NULL) 8567 return FALSE; 8568 } 8569 if (runlen < sortlen) 8570 { 8571 memcpy (buf, p, runlen); 8572 memmove (loc + runlen, loc, sortlen); 8573 memcpy (loc, buf, runlen); 8574 } 8575 else 8576 { 8577 memcpy (buf, loc, sortlen); 8578 memmove (loc, p, runlen); 8579 memcpy (loc + runlen, buf, sortlen); 8580 } 8581 p += runlen - elt_size; 8582 } 8583 } 8584 /* Hashes are no longer valid. */ 8585 free (reldata->hashes); 8586 reldata->hashes = NULL; 8587 free (buf); 8588 } 8589 return TRUE; 8590} 8591 8592struct elf_link_sort_rela 8593{ 8594 union { 8595 bfd_vma offset; 8596 bfd_vma sym_mask; 8597 } u; 8598 enum elf_reloc_type_class type; 8599 /* We use this as an array of size int_rels_per_ext_rel. */ 8600 Elf_Internal_Rela rela[1]; 8601}; 8602 8603static int 8604elf_link_sort_cmp1 (const void *A, const void *B) 8605{ 8606 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; 8607 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; 8608 int relativea, relativeb; 8609 8610 relativea = a->type == reloc_class_relative; 8611 relativeb = b->type == reloc_class_relative; 8612 8613 if (relativea < relativeb) 8614 return 1; 8615 if (relativea > relativeb) 8616 return -1; 8617 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) 8618 return -1; 8619 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) 8620 return 1; 8621 if (a->rela->r_offset < b->rela->r_offset) 8622 return -1; 8623 if (a->rela->r_offset > b->rela->r_offset) 8624 return 1; 8625 return 0; 8626} 8627 8628static int 8629elf_link_sort_cmp2 (const void *A, const void *B) 8630{ 8631 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; 8632 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; 8633 8634 if (a->type < b->type) 8635 return -1; 8636 if (a->type > b->type) 8637 return 1; 8638 if (a->u.offset < b->u.offset) 8639 return -1; 8640 if (a->u.offset > b->u.offset) 8641 return 1; 8642 if (a->rela->r_offset < b->rela->r_offset) 8643 return -1; 8644 if (a->rela->r_offset > b->rela->r_offset) 8645 return 1; 8646 return 0; 8647} 8648 8649static size_t 8650elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) 8651{ 8652 asection *dynamic_relocs; 8653 asection *rela_dyn; 8654 asection *rel_dyn; 8655 bfd_size_type count, size; 8656 size_t i, ret, sort_elt, ext_size; 8657 bfd_byte *sort, *s_non_relative, *p; 8658 struct elf_link_sort_rela *sq; 8659 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8660 int i2e = bed->s->int_rels_per_ext_rel; 8661 unsigned int opb = bfd_octets_per_byte (abfd); 8662 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 8663 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 8664 struct bfd_link_order *lo; 8665 bfd_vma r_sym_mask; 8666 bfd_boolean use_rela; 8667 8668 /* Find a dynamic reloc section. */ 8669 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); 8670 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); 8671 if (rela_dyn != NULL && rela_dyn->size > 0 8672 && rel_dyn != NULL && rel_dyn->size > 0) 8673 { 8674 bfd_boolean use_rela_initialised = FALSE; 8675 8676 /* This is just here to stop gcc from complaining. 8677 Its initialization checking code is not perfect. */ 8678 use_rela = TRUE; 8679 8680 /* Both sections are present. Examine the sizes 8681 of the indirect sections to help us choose. */ 8682 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) 8683 if (lo->type == bfd_indirect_link_order) 8684 { 8685 asection *o = lo->u.indirect.section; 8686 8687 if ((o->size % bed->s->sizeof_rela) == 0) 8688 { 8689 if ((o->size % bed->s->sizeof_rel) == 0) 8690 /* Section size is divisible by both rel and rela sizes. 8691 It is of no help to us. */ 8692 ; 8693 else 8694 { 8695 /* Section size is only divisible by rela. */ 8696 if (use_rela_initialised && (use_rela == FALSE)) 8697 { 8698 _bfd_error_handler (_("%B: Unable to sort relocs - " 8699 "they are in more than one size"), 8700 abfd); 8701 bfd_set_error (bfd_error_invalid_operation); 8702 return 0; 8703 } 8704 else 8705 { 8706 use_rela = TRUE; 8707 use_rela_initialised = TRUE; 8708 } 8709 } 8710 } 8711 else if ((o->size % bed->s->sizeof_rel) == 0) 8712 { 8713 /* Section size is only divisible by rel. */ 8714 if (use_rela_initialised && (use_rela == TRUE)) 8715 { 8716 _bfd_error_handler (_("%B: Unable to sort relocs - " 8717 "they are in more than one size"), 8718 abfd); 8719 bfd_set_error (bfd_error_invalid_operation); 8720 return 0; 8721 } 8722 else 8723 { 8724 use_rela = FALSE; 8725 use_rela_initialised = TRUE; 8726 } 8727 } 8728 else 8729 { 8730 /* The section size is not divisible by either - 8731 something is wrong. */ 8732 _bfd_error_handler (_("%B: Unable to sort relocs - " 8733 "they are of an unknown size"), abfd); 8734 bfd_set_error (bfd_error_invalid_operation); 8735 return 0; 8736 } 8737 } 8738 8739 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) 8740 if (lo->type == bfd_indirect_link_order) 8741 { 8742 asection *o = lo->u.indirect.section; 8743 8744 if ((o->size % bed->s->sizeof_rela) == 0) 8745 { 8746 if ((o->size % bed->s->sizeof_rel) == 0) 8747 /* Section size is divisible by both rel and rela sizes. 8748 It is of no help to us. */ 8749 ; 8750 else 8751 { 8752 /* Section size is only divisible by rela. */ 8753 if (use_rela_initialised && (use_rela == FALSE)) 8754 { 8755 _bfd_error_handler (_("%B: Unable to sort relocs - " 8756 "they are in more than one size"), 8757 abfd); 8758 bfd_set_error (bfd_error_invalid_operation); 8759 return 0; 8760 } 8761 else 8762 { 8763 use_rela = TRUE; 8764 use_rela_initialised = TRUE; 8765 } 8766 } 8767 } 8768 else if ((o->size % bed->s->sizeof_rel) == 0) 8769 { 8770 /* Section size is only divisible by rel. */ 8771 if (use_rela_initialised && (use_rela == TRUE)) 8772 { 8773 _bfd_error_handler (_("%B: Unable to sort relocs - " 8774 "they are in more than one size"), 8775 abfd); 8776 bfd_set_error (bfd_error_invalid_operation); 8777 return 0; 8778 } 8779 else 8780 { 8781 use_rela = FALSE; 8782 use_rela_initialised = TRUE; 8783 } 8784 } 8785 else 8786 { 8787 /* The section size is not divisible by either - 8788 something is wrong. */ 8789 _bfd_error_handler (_("%B: Unable to sort relocs - " 8790 "they are of an unknown size"), abfd); 8791 bfd_set_error (bfd_error_invalid_operation); 8792 return 0; 8793 } 8794 } 8795 8796 if (! use_rela_initialised) 8797 /* Make a guess. */ 8798 use_rela = TRUE; 8799 } 8800 else if (rela_dyn != NULL && rela_dyn->size > 0) 8801 use_rela = TRUE; 8802 else if (rel_dyn != NULL && rel_dyn->size > 0) 8803 use_rela = FALSE; 8804 else 8805 return 0; 8806 8807 if (use_rela) 8808 { 8809 dynamic_relocs = rela_dyn; 8810 ext_size = bed->s->sizeof_rela; 8811 swap_in = bed->s->swap_reloca_in; 8812 swap_out = bed->s->swap_reloca_out; 8813 } 8814 else 8815 { 8816 dynamic_relocs = rel_dyn; 8817 ext_size = bed->s->sizeof_rel; 8818 swap_in = bed->s->swap_reloc_in; 8819 swap_out = bed->s->swap_reloc_out; 8820 } 8821 8822 size = 0; 8823 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 8824 if (lo->type == bfd_indirect_link_order) 8825 size += lo->u.indirect.section->size; 8826 8827 if (size != dynamic_relocs->size) 8828 return 0; 8829 8830 sort_elt = (sizeof (struct elf_link_sort_rela) 8831 + (i2e - 1) * sizeof (Elf_Internal_Rela)); 8832 8833 count = dynamic_relocs->size / ext_size; 8834 if (count == 0) 8835 return 0; 8836 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); 8837 8838 if (sort == NULL) 8839 { 8840 (*info->callbacks->warning) 8841 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); 8842 return 0; 8843 } 8844 8845 if (bed->s->arch_size == 32) 8846 r_sym_mask = ~(bfd_vma) 0xff; 8847 else 8848 r_sym_mask = ~(bfd_vma) 0xffffffff; 8849 8850 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 8851 if (lo->type == bfd_indirect_link_order) 8852 { 8853 bfd_byte *erel, *erelend; 8854 asection *o = lo->u.indirect.section; 8855 8856 if (o->contents == NULL && o->size != 0) 8857 { 8858 /* This is a reloc section that is being handled as a normal 8859 section. See bfd_section_from_shdr. We can't combine 8860 relocs in this case. */ 8861 free (sort); 8862 return 0; 8863 } 8864 erel = o->contents; 8865 erelend = o->contents + o->size; 8866 p = sort + o->output_offset * opb / ext_size * sort_elt; 8867 8868 while (erel < erelend) 8869 { 8870 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 8871 8872 (*swap_in) (abfd, erel, s->rela); 8873 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); 8874 s->u.sym_mask = r_sym_mask; 8875 p += sort_elt; 8876 erel += ext_size; 8877 } 8878 } 8879 8880 qsort (sort, count, sort_elt, elf_link_sort_cmp1); 8881 8882 for (i = 0, p = sort; i < count; i++, p += sort_elt) 8883 { 8884 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 8885 if (s->type != reloc_class_relative) 8886 break; 8887 } 8888 ret = i; 8889 s_non_relative = p; 8890 8891 sq = (struct elf_link_sort_rela *) s_non_relative; 8892 for (; i < count; i++, p += sort_elt) 8893 { 8894 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; 8895 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) 8896 sq = sp; 8897 sp->u.offset = sq->rela->r_offset; 8898 } 8899 8900 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); 8901 8902 struct elf_link_hash_table *htab = elf_hash_table (info); 8903 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) 8904 { 8905 /* We have plt relocs in .rela.dyn. */ 8906 sq = (struct elf_link_sort_rela *) sort; 8907 for (i = 0; i < count; i++) 8908 if (sq[count - i - 1].type != reloc_class_plt) 8909 break; 8910 if (i != 0 && htab->srelplt->size == i * ext_size) 8911 { 8912 struct bfd_link_order **plo; 8913 /* Put srelplt link_order last. This is so the output_offset 8914 set in the next loop is correct for DT_JMPREL. */ 8915 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) 8916 if ((*plo)->type == bfd_indirect_link_order 8917 && (*plo)->u.indirect.section == htab->srelplt) 8918 { 8919 lo = *plo; 8920 *plo = lo->next; 8921 } 8922 else 8923 plo = &(*plo)->next; 8924 *plo = lo; 8925 lo->next = NULL; 8926 dynamic_relocs->map_tail.link_order = lo; 8927 } 8928 } 8929 8930 p = sort; 8931 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 8932 if (lo->type == bfd_indirect_link_order) 8933 { 8934 bfd_byte *erel, *erelend; 8935 asection *o = lo->u.indirect.section; 8936 8937 erel = o->contents; 8938 erelend = o->contents + o->size; 8939 o->output_offset = (p - sort) / sort_elt * ext_size / opb; 8940 while (erel < erelend) 8941 { 8942 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 8943 (*swap_out) (abfd, s->rela, erel); 8944 p += sort_elt; 8945 erel += ext_size; 8946 } 8947 } 8948 8949 free (sort); 8950 *psec = dynamic_relocs; 8951 return ret; 8952} 8953 8954/* Add a symbol to the output symbol string table. */ 8955 8956static int 8957elf_link_output_symstrtab (struct elf_final_link_info *flinfo, 8958 const char *name, 8959 Elf_Internal_Sym *elfsym, 8960 asection *input_sec, 8961 struct elf_link_hash_entry *h) 8962{ 8963 int (*output_symbol_hook) 8964 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, 8965 struct elf_link_hash_entry *); 8966 struct elf_link_hash_table *hash_table; 8967 const struct elf_backend_data *bed; 8968 bfd_size_type strtabsize; 8969 8970 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); 8971 8972 bed = get_elf_backend_data (flinfo->output_bfd); 8973 output_symbol_hook = bed->elf_backend_link_output_symbol_hook; 8974 if (output_symbol_hook != NULL) 8975 { 8976 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); 8977 if (ret != 1) 8978 return ret; 8979 } 8980 8981 if (name == NULL 8982 || *name == '\0' 8983 || (input_sec->flags & SEC_EXCLUDE)) 8984 elfsym->st_name = (unsigned long) -1; 8985 else 8986 { 8987 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize 8988 to get the final offset for st_name. */ 8989 elfsym->st_name 8990 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, 8991 name, FALSE); 8992 if (elfsym->st_name == (unsigned long) -1) 8993 return 0; 8994 } 8995 8996 hash_table = elf_hash_table (flinfo->info); 8997 strtabsize = hash_table->strtabsize; 8998 if (strtabsize <= hash_table->strtabcount) 8999 { 9000 strtabsize += strtabsize; 9001 hash_table->strtabsize = strtabsize; 9002 strtabsize *= sizeof (*hash_table->strtab); 9003 hash_table->strtab 9004 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, 9005 strtabsize); 9006 if (hash_table->strtab == NULL) 9007 return 0; 9008 } 9009 hash_table->strtab[hash_table->strtabcount].sym = *elfsym; 9010 hash_table->strtab[hash_table->strtabcount].dest_index 9011 = hash_table->strtabcount; 9012 hash_table->strtab[hash_table->strtabcount].destshndx_index 9013 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; 9014 9015 bfd_get_symcount (flinfo->output_bfd) += 1; 9016 hash_table->strtabcount += 1; 9017 9018 return 1; 9019} 9020 9021/* Swap symbols out to the symbol table and flush the output symbols to 9022 the file. */ 9023 9024static bfd_boolean 9025elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) 9026{ 9027 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); 9028 bfd_size_type amt; 9029 size_t i; 9030 const struct elf_backend_data *bed; 9031 bfd_byte *symbuf; 9032 Elf_Internal_Shdr *hdr; 9033 file_ptr pos; 9034 bfd_boolean ret; 9035 9036 if (!hash_table->strtabcount) 9037 return TRUE; 9038 9039 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); 9040 9041 bed = get_elf_backend_data (flinfo->output_bfd); 9042 9043 amt = bed->s->sizeof_sym * hash_table->strtabcount; 9044 symbuf = (bfd_byte *) bfd_malloc (amt); 9045 if (symbuf == NULL) 9046 return FALSE; 9047 9048 if (flinfo->symshndxbuf) 9049 { 9050 amt = sizeof (Elf_External_Sym_Shndx); 9051 amt *= bfd_get_symcount (flinfo->output_bfd); 9052 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); 9053 if (flinfo->symshndxbuf == NULL) 9054 { 9055 free (symbuf); 9056 return FALSE; 9057 } 9058 } 9059 9060 for (i = 0; i < hash_table->strtabcount; i++) 9061 { 9062 struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; 9063 if (elfsym->sym.st_name == (unsigned long) -1) 9064 elfsym->sym.st_name = 0; 9065 else 9066 elfsym->sym.st_name 9067 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, 9068 elfsym->sym.st_name); 9069 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, 9070 ((bfd_byte *) symbuf 9071 + (elfsym->dest_index 9072 * bed->s->sizeof_sym)), 9073 (flinfo->symshndxbuf 9074 + elfsym->destshndx_index)); 9075 } 9076 9077 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; 9078 pos = hdr->sh_offset + hdr->sh_size; 9079 amt = hash_table->strtabcount * bed->s->sizeof_sym; 9080 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 9081 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) 9082 { 9083 hdr->sh_size += amt; 9084 ret = TRUE; 9085 } 9086 else 9087 ret = FALSE; 9088 9089 free (symbuf); 9090 9091 free (hash_table->strtab); 9092 hash_table->strtab = NULL; 9093 9094 return ret; 9095} 9096 9097/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ 9098 9099static bfd_boolean 9100check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) 9101{ 9102 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) 9103 && sym->st_shndx < SHN_LORESERVE) 9104 { 9105 /* The gABI doesn't support dynamic symbols in output sections 9106 beyond 64k. */ 9107 _bfd_error_handler 9108 /* xgettext:c-format */ 9109 (_("%B: Too many sections: %d (>= %d)"), 9110 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); 9111 bfd_set_error (bfd_error_nonrepresentable_section); 9112 return FALSE; 9113 } 9114 return TRUE; 9115} 9116 9117/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in 9118 allowing an unsatisfied unversioned symbol in the DSO to match a 9119 versioned symbol that would normally require an explicit version. 9120 We also handle the case that a DSO references a hidden symbol 9121 which may be satisfied by a versioned symbol in another DSO. */ 9122 9123static bfd_boolean 9124elf_link_check_versioned_symbol (struct bfd_link_info *info, 9125 const struct elf_backend_data *bed, 9126 struct elf_link_hash_entry *h) 9127{ 9128 bfd *abfd; 9129 struct elf_link_loaded_list *loaded; 9130 9131 if (!is_elf_hash_table (info->hash)) 9132 return FALSE; 9133 9134 /* Check indirect symbol. */ 9135 while (h->root.type == bfd_link_hash_indirect) 9136 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9137 9138 switch (h->root.type) 9139 { 9140 default: 9141 abfd = NULL; 9142 break; 9143 9144 case bfd_link_hash_undefined: 9145 case bfd_link_hash_undefweak: 9146 abfd = h->root.u.undef.abfd; 9147 if (abfd == NULL 9148 || (abfd->flags & DYNAMIC) == 0 9149 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) 9150 return FALSE; 9151 break; 9152 9153 case bfd_link_hash_defined: 9154 case bfd_link_hash_defweak: 9155 abfd = h->root.u.def.section->owner; 9156 break; 9157 9158 case bfd_link_hash_common: 9159 abfd = h->root.u.c.p->section->owner; 9160 break; 9161 } 9162 BFD_ASSERT (abfd != NULL); 9163 9164 for (loaded = elf_hash_table (info)->loaded; 9165 loaded != NULL; 9166 loaded = loaded->next) 9167 { 9168 bfd *input; 9169 Elf_Internal_Shdr *hdr; 9170 size_t symcount; 9171 size_t extsymcount; 9172 size_t extsymoff; 9173 Elf_Internal_Shdr *versymhdr; 9174 Elf_Internal_Sym *isym; 9175 Elf_Internal_Sym *isymend; 9176 Elf_Internal_Sym *isymbuf; 9177 Elf_External_Versym *ever; 9178 Elf_External_Versym *extversym; 9179 9180 input = loaded->abfd; 9181 9182 /* We check each DSO for a possible hidden versioned definition. */ 9183 if (input == abfd 9184 || (input->flags & DYNAMIC) == 0 9185 || elf_dynversym (input) == 0) 9186 continue; 9187 9188 hdr = &elf_tdata (input)->dynsymtab_hdr; 9189 9190 symcount = hdr->sh_size / bed->s->sizeof_sym; 9191 if (elf_bad_symtab (input)) 9192 { 9193 extsymcount = symcount; 9194 extsymoff = 0; 9195 } 9196 else 9197 { 9198 extsymcount = symcount - hdr->sh_info; 9199 extsymoff = hdr->sh_info; 9200 } 9201 9202 if (extsymcount == 0) 9203 continue; 9204 9205 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, 9206 NULL, NULL, NULL); 9207 if (isymbuf == NULL) 9208 return FALSE; 9209 9210 /* Read in any version definitions. */ 9211 versymhdr = &elf_tdata (input)->dynversym_hdr; 9212 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); 9213 if (extversym == NULL) 9214 goto error_ret; 9215 9216 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 9217 || (bfd_bread (extversym, versymhdr->sh_size, input) 9218 != versymhdr->sh_size)) 9219 { 9220 free (extversym); 9221 error_ret: 9222 free (isymbuf); 9223 return FALSE; 9224 } 9225 9226 ever = extversym + extsymoff; 9227 isymend = isymbuf + extsymcount; 9228 for (isym = isymbuf; isym < isymend; isym++, ever++) 9229 { 9230 const char *name; 9231 Elf_Internal_Versym iver; 9232 unsigned short version_index; 9233 9234 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL 9235 || isym->st_shndx == SHN_UNDEF) 9236 continue; 9237 9238 name = bfd_elf_string_from_elf_section (input, 9239 hdr->sh_link, 9240 isym->st_name); 9241 if (strcmp (name, h->root.root.string) != 0) 9242 continue; 9243 9244 _bfd_elf_swap_versym_in (input, ever, &iver); 9245 9246 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 9247 && !(h->def_regular 9248 && h->forced_local)) 9249 { 9250 /* If we have a non-hidden versioned sym, then it should 9251 have provided a definition for the undefined sym unless 9252 it is defined in a non-shared object and forced local. 9253 */ 9254 abort (); 9255 } 9256 9257 version_index = iver.vs_vers & VERSYM_VERSION; 9258 if (version_index == 1 || version_index == 2) 9259 { 9260 /* This is the base or first version. We can use it. */ 9261 free (extversym); 9262 free (isymbuf); 9263 return TRUE; 9264 } 9265 } 9266 9267 free (extversym); 9268 free (isymbuf); 9269 } 9270 9271 return FALSE; 9272} 9273 9274/* Convert ELF common symbol TYPE. */ 9275 9276static int 9277elf_link_convert_common_type (struct bfd_link_info *info, int type) 9278{ 9279 /* Commom symbol can only appear in relocatable link. */ 9280 if (!bfd_link_relocatable (info)) 9281 abort (); 9282 switch (info->elf_stt_common) 9283 { 9284 case unchanged: 9285 break; 9286 case elf_stt_common: 9287 type = STT_COMMON; 9288 break; 9289 case no_elf_stt_common: 9290 type = STT_OBJECT; 9291 break; 9292 } 9293 return type; 9294} 9295 9296/* Add an external symbol to the symbol table. This is called from 9297 the hash table traversal routine. When generating a shared object, 9298 we go through the symbol table twice. The first time we output 9299 anything that might have been forced to local scope in a version 9300 script. The second time we output the symbols that are still 9301 global symbols. */ 9302 9303static bfd_boolean 9304elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) 9305{ 9306 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; 9307 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; 9308 struct elf_final_link_info *flinfo = eoinfo->flinfo; 9309 bfd_boolean strip; 9310 Elf_Internal_Sym sym; 9311 asection *input_sec; 9312 const struct elf_backend_data *bed; 9313 long indx; 9314 int ret; 9315 unsigned int type; 9316 9317 if (h->root.type == bfd_link_hash_warning) 9318 { 9319 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9320 if (h->root.type == bfd_link_hash_new) 9321 return TRUE; 9322 } 9323 9324 /* Decide whether to output this symbol in this pass. */ 9325 if (eoinfo->localsyms) 9326 { 9327 if (!h->forced_local) 9328 return TRUE; 9329 } 9330 else 9331 { 9332 if (h->forced_local) 9333 return TRUE; 9334 } 9335 9336 bed = get_elf_backend_data (flinfo->output_bfd); 9337 9338 if (h->root.type == bfd_link_hash_undefined) 9339 { 9340 /* If we have an undefined symbol reference here then it must have 9341 come from a shared library that is being linked in. (Undefined 9342 references in regular files have already been handled unless 9343 they are in unreferenced sections which are removed by garbage 9344 collection). */ 9345 bfd_boolean ignore_undef = FALSE; 9346 9347 /* Some symbols may be special in that the fact that they're 9348 undefined can be safely ignored - let backend determine that. */ 9349 if (bed->elf_backend_ignore_undef_symbol) 9350 ignore_undef = bed->elf_backend_ignore_undef_symbol (h); 9351 9352 /* If we are reporting errors for this situation then do so now. */ 9353 if (!ignore_undef 9354 && h->ref_dynamic 9355 && (!h->ref_regular || flinfo->info->gc_sections) 9356 && !elf_link_check_versioned_symbol (flinfo->info, bed, h) 9357 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) 9358 (*flinfo->info->callbacks->undefined_symbol) 9359 (flinfo->info, h->root.root.string, 9360 h->ref_regular ? NULL : h->root.u.undef.abfd, 9361 NULL, 0, 9362 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR); 9363 9364 /* Strip a global symbol defined in a discarded section. */ 9365 if (h->indx == -3) 9366 return TRUE; 9367 } 9368 9369 /* We should also warn if a forced local symbol is referenced from 9370 shared libraries. */ 9371 if (bfd_link_executable (flinfo->info) 9372 && h->forced_local 9373 && h->ref_dynamic 9374 && h->def_regular 9375 && !h->dynamic_def 9376 && h->ref_dynamic_nonweak 9377 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) 9378 { 9379 bfd *def_bfd; 9380 const char *msg; 9381 struct elf_link_hash_entry *hi = h; 9382 9383 /* Check indirect symbol. */ 9384 while (hi->root.type == bfd_link_hash_indirect) 9385 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 9386 9387 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) 9388 /* xgettext:c-format */ 9389 msg = _("%B: internal symbol `%s' in %B is referenced by DSO"); 9390 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) 9391 /* xgettext:c-format */ 9392 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO"); 9393 else 9394 /* xgettext:c-format */ 9395 msg = _("%B: local symbol `%s' in %B is referenced by DSO"); 9396 def_bfd = flinfo->output_bfd; 9397 if (hi->root.u.def.section != bfd_abs_section_ptr) 9398 def_bfd = hi->root.u.def.section->owner; 9399 _bfd_error_handler (msg, flinfo->output_bfd, def_bfd, 9400 h->root.root.string); 9401 bfd_set_error (bfd_error_bad_value); 9402 eoinfo->failed = TRUE; 9403 return FALSE; 9404 } 9405 9406 /* We don't want to output symbols that have never been mentioned by 9407 a regular file, or that we have been told to strip. However, if 9408 h->indx is set to -2, the symbol is used by a reloc and we must 9409 output it. */ 9410 strip = FALSE; 9411 if (h->indx == -2) 9412 ; 9413 else if ((h->def_dynamic 9414 || h->ref_dynamic 9415 || h->root.type == bfd_link_hash_new) 9416 && !h->def_regular 9417 && !h->ref_regular) 9418 strip = TRUE; 9419 else if (flinfo->info->strip == strip_all) 9420 strip = TRUE; 9421 else if (flinfo->info->strip == strip_some 9422 && bfd_hash_lookup (flinfo->info->keep_hash, 9423 h->root.root.string, FALSE, FALSE) == NULL) 9424 strip = TRUE; 9425 else if ((h->root.type == bfd_link_hash_defined 9426 || h->root.type == bfd_link_hash_defweak) 9427 && ((flinfo->info->strip_discarded 9428 && discarded_section (h->root.u.def.section)) 9429 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 9430 && h->root.u.def.section->owner != NULL 9431 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) 9432 strip = TRUE; 9433 else if ((h->root.type == bfd_link_hash_undefined 9434 || h->root.type == bfd_link_hash_undefweak) 9435 && h->root.u.undef.abfd != NULL 9436 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) 9437 strip = TRUE; 9438 9439 type = h->type; 9440 9441 /* If we're stripping it, and it's not a dynamic symbol, there's 9442 nothing else to do. However, if it is a forced local symbol or 9443 an ifunc symbol we need to give the backend finish_dynamic_symbol 9444 function a chance to make it dynamic. */ 9445 if (strip 9446 && h->dynindx == -1 9447 && type != STT_GNU_IFUNC 9448 && !h->forced_local) 9449 return TRUE; 9450 9451 sym.st_value = 0; 9452 sym.st_size = h->size; 9453 sym.st_other = h->other; 9454 switch (h->root.type) 9455 { 9456 default: 9457 case bfd_link_hash_new: 9458 case bfd_link_hash_warning: 9459 abort (); 9460 return FALSE; 9461 9462 case bfd_link_hash_undefined: 9463 case bfd_link_hash_undefweak: 9464 input_sec = bfd_und_section_ptr; 9465 sym.st_shndx = SHN_UNDEF; 9466 break; 9467 9468 case bfd_link_hash_defined: 9469 case bfd_link_hash_defweak: 9470 { 9471 input_sec = h->root.u.def.section; 9472 if (input_sec->output_section != NULL) 9473 { 9474 sym.st_shndx = 9475 _bfd_elf_section_from_bfd_section (flinfo->output_bfd, 9476 input_sec->output_section); 9477 if (sym.st_shndx == SHN_BAD) 9478 { 9479 _bfd_error_handler 9480 /* xgettext:c-format */ 9481 (_("%B: could not find output section %A for input section %A"), 9482 flinfo->output_bfd, input_sec->output_section, input_sec); 9483 bfd_set_error (bfd_error_nonrepresentable_section); 9484 eoinfo->failed = TRUE; 9485 return FALSE; 9486 } 9487 9488 /* ELF symbols in relocatable files are section relative, 9489 but in nonrelocatable files they are virtual 9490 addresses. */ 9491 sym.st_value = h->root.u.def.value + input_sec->output_offset; 9492 if (!bfd_link_relocatable (flinfo->info)) 9493 { 9494 sym.st_value += input_sec->output_section->vma; 9495 if (h->type == STT_TLS) 9496 { 9497 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; 9498 if (tls_sec != NULL) 9499 sym.st_value -= tls_sec->vma; 9500 } 9501 } 9502 } 9503 else 9504 { 9505 BFD_ASSERT (input_sec->owner == NULL 9506 || (input_sec->owner->flags & DYNAMIC) != 0); 9507 sym.st_shndx = SHN_UNDEF; 9508 input_sec = bfd_und_section_ptr; 9509 } 9510 } 9511 break; 9512 9513 case bfd_link_hash_common: 9514 input_sec = h->root.u.c.p->section; 9515 sym.st_shndx = bed->common_section_index (input_sec); 9516 sym.st_value = 1 << h->root.u.c.p->alignment_power; 9517 break; 9518 9519 case bfd_link_hash_indirect: 9520 /* These symbols are created by symbol versioning. They point 9521 to the decorated version of the name. For example, if the 9522 symbol foo@@GNU_1.2 is the default, which should be used when 9523 foo is used with no version, then we add an indirect symbol 9524 foo which points to foo@@GNU_1.2. We ignore these symbols, 9525 since the indirected symbol is already in the hash table. */ 9526 return TRUE; 9527 } 9528 9529 if (type == STT_COMMON || type == STT_OBJECT) 9530 switch (h->root.type) 9531 { 9532 case bfd_link_hash_common: 9533 type = elf_link_convert_common_type (flinfo->info, type); 9534 break; 9535 case bfd_link_hash_defined: 9536 case bfd_link_hash_defweak: 9537 if (bed->common_definition (&sym)) 9538 type = elf_link_convert_common_type (flinfo->info, type); 9539 else 9540 type = STT_OBJECT; 9541 break; 9542 case bfd_link_hash_undefined: 9543 case bfd_link_hash_undefweak: 9544 break; 9545 default: 9546 abort (); 9547 } 9548 9549 if (h->forced_local) 9550 { 9551 sym.st_info = ELF_ST_INFO (STB_LOCAL, type); 9552 /* Turn off visibility on local symbol. */ 9553 sym.st_other &= ~ELF_ST_VISIBILITY (-1); 9554 } 9555 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ 9556 else if (h->unique_global && h->def_regular) 9557 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); 9558 else if (h->root.type == bfd_link_hash_undefweak 9559 || h->root.type == bfd_link_hash_defweak) 9560 sym.st_info = ELF_ST_INFO (STB_WEAK, type); 9561 else 9562 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 9563 sym.st_target_internal = h->target_internal; 9564 9565 /* Give the processor backend a chance to tweak the symbol value, 9566 and also to finish up anything that needs to be done for this 9567 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for 9568 forced local syms when non-shared is due to a historical quirk. 9569 STT_GNU_IFUNC symbol must go through PLT. */ 9570 if ((h->type == STT_GNU_IFUNC 9571 && h->def_regular 9572 && !bfd_link_relocatable (flinfo->info)) 9573 || ((h->dynindx != -1 9574 || h->forced_local) 9575 && ((bfd_link_pic (flinfo->info) 9576 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 9577 || h->root.type != bfd_link_hash_undefweak)) 9578 || !h->forced_local) 9579 && elf_hash_table (flinfo->info)->dynamic_sections_created)) 9580 { 9581 if (! ((*bed->elf_backend_finish_dynamic_symbol) 9582 (flinfo->output_bfd, flinfo->info, h, &sym))) 9583 { 9584 eoinfo->failed = TRUE; 9585 return FALSE; 9586 } 9587 } 9588 9589 /* If we are marking the symbol as undefined, and there are no 9590 non-weak references to this symbol from a regular object, then 9591 mark the symbol as weak undefined; if there are non-weak 9592 references, mark the symbol as strong. We can't do this earlier, 9593 because it might not be marked as undefined until the 9594 finish_dynamic_symbol routine gets through with it. */ 9595 if (sym.st_shndx == SHN_UNDEF 9596 && h->ref_regular 9597 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL 9598 || ELF_ST_BIND (sym.st_info) == STB_WEAK)) 9599 { 9600 int bindtype; 9601 type = ELF_ST_TYPE (sym.st_info); 9602 9603 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ 9604 if (type == STT_GNU_IFUNC) 9605 type = STT_FUNC; 9606 9607 if (h->ref_regular_nonweak) 9608 bindtype = STB_GLOBAL; 9609 else 9610 bindtype = STB_WEAK; 9611 sym.st_info = ELF_ST_INFO (bindtype, type); 9612 } 9613 9614 /* If this is a symbol defined in a dynamic library, don't use the 9615 symbol size from the dynamic library. Relinking an executable 9616 against a new library may introduce gratuitous changes in the 9617 executable's symbols if we keep the size. */ 9618 if (sym.st_shndx == SHN_UNDEF 9619 && !h->def_regular 9620 && h->def_dynamic) 9621 sym.st_size = 0; 9622 9623 /* If a non-weak symbol with non-default visibility is not defined 9624 locally, it is a fatal error. */ 9625 if (!bfd_link_relocatable (flinfo->info) 9626 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT 9627 && ELF_ST_BIND (sym.st_info) != STB_WEAK 9628 && h->root.type == bfd_link_hash_undefined 9629 && !h->def_regular) 9630 { 9631 const char *msg; 9632 9633 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) 9634 /* xgettext:c-format */ 9635 msg = _("%B: protected symbol `%s' isn't defined"); 9636 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) 9637 /* xgettext:c-format */ 9638 msg = _("%B: internal symbol `%s' isn't defined"); 9639 else 9640 /* xgettext:c-format */ 9641 msg = _("%B: hidden symbol `%s' isn't defined"); 9642 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); 9643 bfd_set_error (bfd_error_bad_value); 9644 eoinfo->failed = TRUE; 9645 return FALSE; 9646 } 9647 9648 /* If this symbol should be put in the .dynsym section, then put it 9649 there now. We already know the symbol index. We also fill in 9650 the entry in the .hash section. */ 9651 if (elf_hash_table (flinfo->info)->dynsym != NULL 9652 && h->dynindx != -1 9653 && elf_hash_table (flinfo->info)->dynamic_sections_created) 9654 { 9655 bfd_byte *esym; 9656 9657 /* Since there is no version information in the dynamic string, 9658 if there is no version info in symbol version section, we will 9659 have a run-time problem if not linking executable, referenced 9660 by shared library, or not bound locally. */ 9661 if (h->verinfo.verdef == NULL 9662 && (!bfd_link_executable (flinfo->info) 9663 || h->ref_dynamic 9664 || !h->def_regular)) 9665 { 9666 char *p = strrchr (h->root.root.string, ELF_VER_CHR); 9667 9668 if (p && p [1] != '\0') 9669 { 9670 _bfd_error_handler 9671 /* xgettext:c-format */ 9672 (_("%B: No symbol version section for versioned symbol `%s'"), 9673 flinfo->output_bfd, h->root.root.string); 9674 eoinfo->failed = TRUE; 9675 return FALSE; 9676 } 9677 } 9678 9679 sym.st_name = h->dynstr_index; 9680 esym = (elf_hash_table (flinfo->info)->dynsym->contents 9681 + h->dynindx * bed->s->sizeof_sym); 9682 if (!check_dynsym (flinfo->output_bfd, &sym)) 9683 { 9684 eoinfo->failed = TRUE; 9685 return FALSE; 9686 } 9687 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); 9688 9689 if (flinfo->hash_sec != NULL) 9690 { 9691 size_t hash_entry_size; 9692 bfd_byte *bucketpos; 9693 bfd_vma chain; 9694 size_t bucketcount; 9695 size_t bucket; 9696 9697 bucketcount = elf_hash_table (flinfo->info)->bucketcount; 9698 bucket = h->u.elf_hash_value % bucketcount; 9699 9700 hash_entry_size 9701 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; 9702 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents 9703 + (bucket + 2) * hash_entry_size); 9704 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); 9705 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, 9706 bucketpos); 9707 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, 9708 ((bfd_byte *) flinfo->hash_sec->contents 9709 + (bucketcount + 2 + h->dynindx) * hash_entry_size)); 9710 } 9711 9712 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) 9713 { 9714 Elf_Internal_Versym iversym; 9715 Elf_External_Versym *eversym; 9716 9717 if (!h->def_regular) 9718 { 9719 if (h->verinfo.verdef == NULL 9720 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) 9721 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) 9722 iversym.vs_vers = 0; 9723 else 9724 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; 9725 } 9726 else 9727 { 9728 if (h->verinfo.vertree == NULL) 9729 iversym.vs_vers = 1; 9730 else 9731 iversym.vs_vers = h->verinfo.vertree->vernum + 1; 9732 if (flinfo->info->create_default_symver) 9733 iversym.vs_vers++; 9734 } 9735 9736 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is 9737 defined locally. */ 9738 if (h->versioned == versioned_hidden && h->def_regular) 9739 iversym.vs_vers |= VERSYM_HIDDEN; 9740 9741 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; 9742 eversym += h->dynindx; 9743 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); 9744 } 9745 } 9746 9747 /* If the symbol is undefined, and we didn't output it to .dynsym, 9748 strip it from .symtab too. Obviously we can't do this for 9749 relocatable output or when needed for --emit-relocs. */ 9750 else if (input_sec == bfd_und_section_ptr 9751 && h->indx != -2 9752 && !bfd_link_relocatable (flinfo->info)) 9753 return TRUE; 9754 /* Also strip others that we couldn't earlier due to dynamic symbol 9755 processing. */ 9756 if (strip) 9757 return TRUE; 9758 if ((input_sec->flags & SEC_EXCLUDE) != 0) 9759 return TRUE; 9760 9761 /* Output a FILE symbol so that following locals are not associated 9762 with the wrong input file. We need one for forced local symbols 9763 if we've seen more than one FILE symbol or when we have exactly 9764 one FILE symbol but global symbols are present in a file other 9765 than the one with the FILE symbol. We also need one if linker 9766 defined symbols are present. In practice these conditions are 9767 always met, so just emit the FILE symbol unconditionally. */ 9768 if (eoinfo->localsyms 9769 && !eoinfo->file_sym_done 9770 && eoinfo->flinfo->filesym_count != 0) 9771 { 9772 Elf_Internal_Sym fsym; 9773 9774 memset (&fsym, 0, sizeof (fsym)); 9775 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 9776 fsym.st_shndx = SHN_ABS; 9777 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, 9778 bfd_und_section_ptr, NULL)) 9779 return FALSE; 9780 9781 eoinfo->file_sym_done = TRUE; 9782 } 9783 9784 indx = bfd_get_symcount (flinfo->output_bfd); 9785 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, 9786 input_sec, h); 9787 if (ret == 0) 9788 { 9789 eoinfo->failed = TRUE; 9790 return FALSE; 9791 } 9792 else if (ret == 1) 9793 h->indx = indx; 9794 else if (h->indx == -2) 9795 abort(); 9796 9797 return TRUE; 9798} 9799 9800/* Return TRUE if special handling is done for relocs in SEC against 9801 symbols defined in discarded sections. */ 9802 9803static bfd_boolean 9804elf_section_ignore_discarded_relocs (asection *sec) 9805{ 9806 const struct elf_backend_data *bed; 9807 9808 switch (sec->sec_info_type) 9809 { 9810 case SEC_INFO_TYPE_STABS: 9811 case SEC_INFO_TYPE_EH_FRAME: 9812 case SEC_INFO_TYPE_EH_FRAME_ENTRY: 9813 return TRUE; 9814 default: 9815 break; 9816 } 9817 9818 bed = get_elf_backend_data (sec->owner); 9819 if (bed->elf_backend_ignore_discarded_relocs != NULL 9820 && (*bed->elf_backend_ignore_discarded_relocs) (sec)) 9821 return TRUE; 9822 9823 return FALSE; 9824} 9825 9826/* Return a mask saying how ld should treat relocations in SEC against 9827 symbols defined in discarded sections. If this function returns 9828 COMPLAIN set, ld will issue a warning message. If this function 9829 returns PRETEND set, and the discarded section was link-once and the 9830 same size as the kept link-once section, ld will pretend that the 9831 symbol was actually defined in the kept section. Otherwise ld will 9832 zero the reloc (at least that is the intent, but some cooperation by 9833 the target dependent code is needed, particularly for REL targets). */ 9834 9835unsigned int 9836_bfd_elf_default_action_discarded (asection *sec) 9837{ 9838 if (sec->flags & SEC_DEBUGGING) 9839 return PRETEND; 9840 9841 if (strcmp (".eh_frame", sec->name) == 0) 9842 return 0; 9843 9844 if (strcmp (".gcc_except_table", sec->name) == 0) 9845 return 0; 9846 9847 return COMPLAIN | PRETEND; 9848} 9849 9850/* Find a match between a section and a member of a section group. */ 9851 9852static asection * 9853match_group_member (asection *sec, asection *group, 9854 struct bfd_link_info *info) 9855{ 9856 asection *first = elf_next_in_group (group); 9857 asection *s = first; 9858 9859 while (s != NULL) 9860 { 9861 if (bfd_elf_match_symbols_in_sections (s, sec, info)) 9862 return s; 9863 9864 s = elf_next_in_group (s); 9865 if (s == first) 9866 break; 9867 } 9868 9869 return NULL; 9870} 9871 9872/* Check if the kept section of a discarded section SEC can be used 9873 to replace it. Return the replacement if it is OK. Otherwise return 9874 NULL. */ 9875 9876asection * 9877_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) 9878{ 9879 asection *kept; 9880 9881 kept = sec->kept_section; 9882 if (kept != NULL) 9883 { 9884 if ((kept->flags & SEC_GROUP) != 0) 9885 kept = match_group_member (sec, kept, info); 9886 if (kept != NULL 9887 && ((sec->rawsize != 0 ? sec->rawsize : sec->size) 9888 != (kept->rawsize != 0 ? kept->rawsize : kept->size))) 9889 kept = NULL; 9890 sec->kept_section = kept; 9891 } 9892 return kept; 9893} 9894 9895/* Link an input file into the linker output file. This function 9896 handles all the sections and relocations of the input file at once. 9897 This is so that we only have to read the local symbols once, and 9898 don't have to keep them in memory. */ 9899 9900static bfd_boolean 9901elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) 9902{ 9903 int (*relocate_section) 9904 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, 9905 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); 9906 bfd *output_bfd; 9907 Elf_Internal_Shdr *symtab_hdr; 9908 size_t locsymcount; 9909 size_t extsymoff; 9910 Elf_Internal_Sym *isymbuf; 9911 Elf_Internal_Sym *isym; 9912 Elf_Internal_Sym *isymend; 9913 long *pindex; 9914 asection **ppsection; 9915 asection *o; 9916 const struct elf_backend_data *bed; 9917 struct elf_link_hash_entry **sym_hashes; 9918 bfd_size_type address_size; 9919 bfd_vma r_type_mask; 9920 int r_sym_shift; 9921 bfd_boolean have_file_sym = FALSE; 9922 9923 output_bfd = flinfo->output_bfd; 9924 bed = get_elf_backend_data (output_bfd); 9925 relocate_section = bed->elf_backend_relocate_section; 9926 9927 /* If this is a dynamic object, we don't want to do anything here: 9928 we don't want the local symbols, and we don't want the section 9929 contents. */ 9930 if ((input_bfd->flags & DYNAMIC) != 0) 9931 return TRUE; 9932 9933 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 9934 if (elf_bad_symtab (input_bfd)) 9935 { 9936 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 9937 extsymoff = 0; 9938 } 9939 else 9940 { 9941 locsymcount = symtab_hdr->sh_info; 9942 extsymoff = symtab_hdr->sh_info; 9943 } 9944 9945 /* Read the local symbols. */ 9946 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 9947 if (isymbuf == NULL && locsymcount != 0) 9948 { 9949 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, 9950 flinfo->internal_syms, 9951 flinfo->external_syms, 9952 flinfo->locsym_shndx); 9953 if (isymbuf == NULL) 9954 return FALSE; 9955 } 9956 9957 /* Find local symbol sections and adjust values of symbols in 9958 SEC_MERGE sections. Write out those local symbols we know are 9959 going into the output file. */ 9960 isymend = isymbuf + locsymcount; 9961 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; 9962 isym < isymend; 9963 isym++, pindex++, ppsection++) 9964 { 9965 asection *isec; 9966 const char *name; 9967 Elf_Internal_Sym osym; 9968 long indx; 9969 int ret; 9970 9971 *pindex = -1; 9972 9973 if (elf_bad_symtab (input_bfd)) 9974 { 9975 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) 9976 { 9977 *ppsection = NULL; 9978 continue; 9979 } 9980 } 9981 9982 if (isym->st_shndx == SHN_UNDEF) 9983 isec = bfd_und_section_ptr; 9984 else if (isym->st_shndx == SHN_ABS) 9985 isec = bfd_abs_section_ptr; 9986 else if (isym->st_shndx == SHN_COMMON) 9987 isec = bfd_com_section_ptr; 9988 else 9989 { 9990 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); 9991 if (isec == NULL) 9992 { 9993 /* Don't attempt to output symbols with st_shnx in the 9994 reserved range other than SHN_ABS and SHN_COMMON. */ 9995 *ppsection = NULL; 9996 continue; 9997 } 9998 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE 9999 && ELF_ST_TYPE (isym->st_info) != STT_SECTION) 10000 isym->st_value = 10001 _bfd_merged_section_offset (output_bfd, &isec, 10002 elf_section_data (isec)->sec_info, 10003 isym->st_value); 10004 } 10005 10006 *ppsection = isec; 10007 10008 /* Don't output the first, undefined, symbol. In fact, don't 10009 output any undefined local symbol. */ 10010 if (isec == bfd_und_section_ptr) 10011 continue; 10012 10013 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) 10014 { 10015 /* We never output section symbols. Instead, we use the 10016 section symbol of the corresponding section in the output 10017 file. */ 10018 continue; 10019 } 10020 10021 /* If we are stripping all symbols, we don't want to output this 10022 one. */ 10023 if (flinfo->info->strip == strip_all) 10024 continue; 10025 10026 /* If we are discarding all local symbols, we don't want to 10027 output this one. If we are generating a relocatable output 10028 file, then some of the local symbols may be required by 10029 relocs; we output them below as we discover that they are 10030 needed. */ 10031 if (flinfo->info->discard == discard_all) 10032 continue; 10033 10034 /* If this symbol is defined in a section which we are 10035 discarding, we don't need to keep it. */ 10036 if (isym->st_shndx != SHN_UNDEF 10037 && isym->st_shndx < SHN_LORESERVE 10038 && bfd_section_removed_from_list (output_bfd, 10039 isec->output_section)) 10040 continue; 10041 10042 /* Get the name of the symbol. */ 10043 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, 10044 isym->st_name); 10045 if (name == NULL) 10046 return FALSE; 10047 10048 /* See if we are discarding symbols with this name. */ 10049 if ((flinfo->info->strip == strip_some 10050 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) 10051 == NULL)) 10052 || (((flinfo->info->discard == discard_sec_merge 10053 && (isec->flags & SEC_MERGE) 10054 && !bfd_link_relocatable (flinfo->info)) 10055 || flinfo->info->discard == discard_l) 10056 && bfd_is_local_label_name (input_bfd, name))) 10057 continue; 10058 10059 if (ELF_ST_TYPE (isym->st_info) == STT_FILE) 10060 { 10061 if (input_bfd->lto_output) 10062 /* -flto puts a temp file name here. This means builds 10063 are not reproducible. Discard the symbol. */ 10064 continue; 10065 have_file_sym = TRUE; 10066 flinfo->filesym_count += 1; 10067 } 10068 if (!have_file_sym) 10069 { 10070 /* In the absence of debug info, bfd_find_nearest_line uses 10071 FILE symbols to determine the source file for local 10072 function symbols. Provide a FILE symbol here if input 10073 files lack such, so that their symbols won't be 10074 associated with a previous input file. It's not the 10075 source file, but the best we can do. */ 10076 have_file_sym = TRUE; 10077 flinfo->filesym_count += 1; 10078 memset (&osym, 0, sizeof (osym)); 10079 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 10080 osym.st_shndx = SHN_ABS; 10081 if (!elf_link_output_symstrtab (flinfo, 10082 (input_bfd->lto_output ? NULL 10083 : input_bfd->filename), 10084 &osym, bfd_abs_section_ptr, 10085 NULL)) 10086 return FALSE; 10087 } 10088 10089 osym = *isym; 10090 10091 /* Adjust the section index for the output file. */ 10092 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, 10093 isec->output_section); 10094 if (osym.st_shndx == SHN_BAD) 10095 return FALSE; 10096 10097 /* ELF symbols in relocatable files are section relative, but 10098 in executable files they are virtual addresses. Note that 10099 this code assumes that all ELF sections have an associated 10100 BFD section with a reasonable value for output_offset; below 10101 we assume that they also have a reasonable value for 10102 output_section. Any special sections must be set up to meet 10103 these requirements. */ 10104 osym.st_value += isec->output_offset; 10105 if (!bfd_link_relocatable (flinfo->info)) 10106 { 10107 osym.st_value += isec->output_section->vma; 10108 if (ELF_ST_TYPE (osym.st_info) == STT_TLS) 10109 { 10110 /* STT_TLS symbols are relative to PT_TLS segment base. */ 10111 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL); 10112 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; 10113 } 10114 } 10115 10116 indx = bfd_get_symcount (output_bfd); 10117 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); 10118 if (ret == 0) 10119 return FALSE; 10120 else if (ret == 1) 10121 *pindex = indx; 10122 } 10123 10124 if (bed->s->arch_size == 32) 10125 { 10126 r_type_mask = 0xff; 10127 r_sym_shift = 8; 10128 address_size = 4; 10129 } 10130 else 10131 { 10132 r_type_mask = 0xffffffff; 10133 r_sym_shift = 32; 10134 address_size = 8; 10135 } 10136 10137 /* Relocate the contents of each section. */ 10138 sym_hashes = elf_sym_hashes (input_bfd); 10139 for (o = input_bfd->sections; o != NULL; o = o->next) 10140 { 10141 bfd_byte *contents; 10142 10143 if (! o->linker_mark) 10144 { 10145 /* This section was omitted from the link. */ 10146 continue; 10147 } 10148 10149 if (bfd_link_relocatable (flinfo->info) 10150 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) 10151 { 10152 /* Deal with the group signature symbol. */ 10153 struct bfd_elf_section_data *sec_data = elf_section_data (o); 10154 unsigned long symndx = sec_data->this_hdr.sh_info; 10155 asection *osec = o->output_section; 10156 10157 if (symndx >= locsymcount 10158 || (elf_bad_symtab (input_bfd) 10159 && flinfo->sections[symndx] == NULL)) 10160 { 10161 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; 10162 while (h->root.type == bfd_link_hash_indirect 10163 || h->root.type == bfd_link_hash_warning) 10164 h = (struct elf_link_hash_entry *) h->root.u.i.link; 10165 /* Arrange for symbol to be output. */ 10166 h->indx = -2; 10167 elf_section_data (osec)->this_hdr.sh_info = -2; 10168 } 10169 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) 10170 { 10171 /* We'll use the output section target_index. */ 10172 asection *sec = flinfo->sections[symndx]->output_section; 10173 elf_section_data (osec)->this_hdr.sh_info = sec->target_index; 10174 } 10175 else 10176 { 10177 if (flinfo->indices[symndx] == -1) 10178 { 10179 /* Otherwise output the local symbol now. */ 10180 Elf_Internal_Sym sym = isymbuf[symndx]; 10181 asection *sec = flinfo->sections[symndx]->output_section; 10182 const char *name; 10183 long indx; 10184 int ret; 10185 10186 name = bfd_elf_string_from_elf_section (input_bfd, 10187 symtab_hdr->sh_link, 10188 sym.st_name); 10189 if (name == NULL) 10190 return FALSE; 10191 10192 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, 10193 sec); 10194 if (sym.st_shndx == SHN_BAD) 10195 return FALSE; 10196 10197 sym.st_value += o->output_offset; 10198 10199 indx = bfd_get_symcount (output_bfd); 10200 ret = elf_link_output_symstrtab (flinfo, name, &sym, o, 10201 NULL); 10202 if (ret == 0) 10203 return FALSE; 10204 else if (ret == 1) 10205 flinfo->indices[symndx] = indx; 10206 else 10207 abort (); 10208 } 10209 elf_section_data (osec)->this_hdr.sh_info 10210 = flinfo->indices[symndx]; 10211 } 10212 } 10213 10214 if ((o->flags & SEC_HAS_CONTENTS) == 0 10215 || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) 10216 continue; 10217 10218 if ((o->flags & SEC_LINKER_CREATED) != 0) 10219 { 10220 /* Section was created by _bfd_elf_link_create_dynamic_sections 10221 or somesuch. */ 10222 continue; 10223 } 10224 10225 /* Get the contents of the section. They have been cached by a 10226 relaxation routine. Note that o is a section in an input 10227 file, so the contents field will not have been set by any of 10228 the routines which work on output files. */ 10229 if (elf_section_data (o)->this_hdr.contents != NULL) 10230 { 10231 contents = elf_section_data (o)->this_hdr.contents; 10232 if (bed->caches_rawsize 10233 && o->rawsize != 0 10234 && o->rawsize < o->size) 10235 { 10236 memcpy (flinfo->contents, contents, o->rawsize); 10237 contents = flinfo->contents; 10238 } 10239 } 10240 else 10241 { 10242 contents = flinfo->contents; 10243 if (! bfd_get_full_section_contents (input_bfd, o, &contents)) 10244 return FALSE; 10245 } 10246 10247 if ((o->flags & SEC_RELOC) != 0) 10248 { 10249 Elf_Internal_Rela *internal_relocs; 10250 Elf_Internal_Rela *rel, *relend; 10251 int action_discarded; 10252 int ret; 10253 10254 /* Get the swapped relocs. */ 10255 internal_relocs 10256 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, 10257 flinfo->internal_relocs, FALSE); 10258 if (internal_relocs == NULL 10259 && o->reloc_count > 0) 10260 return FALSE; 10261 10262 /* We need to reverse-copy input .ctors/.dtors sections if 10263 they are placed in .init_array/.finit_array for output. */ 10264 if (o->size > address_size 10265 && ((strncmp (o->name, ".ctors", 6) == 0 10266 && strcmp (o->output_section->name, 10267 ".init_array") == 0) 10268 || (strncmp (o->name, ".dtors", 6) == 0 10269 && strcmp (o->output_section->name, 10270 ".fini_array") == 0)) 10271 && (o->name[6] == 0 || o->name[6] == '.')) 10272 { 10273 if (o->size != o->reloc_count * address_size) 10274 { 10275 _bfd_error_handler 10276 /* xgettext:c-format */ 10277 (_("error: %B: size of section %A is not " 10278 "multiple of address size"), 10279 input_bfd, o); 10280 bfd_set_error (bfd_error_on_input); 10281 return FALSE; 10282 } 10283 o->flags |= SEC_ELF_REVERSE_COPY; 10284 } 10285 10286 action_discarded = -1; 10287 if (!elf_section_ignore_discarded_relocs (o)) 10288 action_discarded = (*bed->action_discarded) (o); 10289 10290 /* Run through the relocs evaluating complex reloc symbols and 10291 looking for relocs against symbols from discarded sections 10292 or section symbols from removed link-once sections. 10293 Complain about relocs against discarded sections. Zero 10294 relocs against removed link-once sections. */ 10295 10296 rel = internal_relocs; 10297 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; 10298 for ( ; rel < relend; rel++) 10299 { 10300 unsigned long r_symndx = rel->r_info >> r_sym_shift; 10301 unsigned int s_type; 10302 asection **ps, *sec; 10303 struct elf_link_hash_entry *h = NULL; 10304 const char *sym_name; 10305 10306 if (r_symndx == STN_UNDEF) 10307 continue; 10308 10309 if (r_symndx >= locsymcount 10310 || (elf_bad_symtab (input_bfd) 10311 && flinfo->sections[r_symndx] == NULL)) 10312 { 10313 h = sym_hashes[r_symndx - extsymoff]; 10314 10315 /* Badly formatted input files can contain relocs that 10316 reference non-existant symbols. Check here so that 10317 we do not seg fault. */ 10318 if (h == NULL) 10319 { 10320 char buffer [32]; 10321 10322 sprintf_vma (buffer, rel->r_info); 10323 _bfd_error_handler 10324 /* xgettext:c-format */ 10325 (_("error: %B contains a reloc (0x%s) for section %A " 10326 "that references a non-existent global symbol"), 10327 input_bfd, o, buffer); 10328 bfd_set_error (bfd_error_bad_value); 10329 return FALSE; 10330 } 10331 10332 while (h->root.type == bfd_link_hash_indirect 10333 || h->root.type == bfd_link_hash_warning) 10334 h = (struct elf_link_hash_entry *) h->root.u.i.link; 10335 10336 s_type = h->type; 10337 10338 /* If a plugin symbol is referenced from a non-IR file, 10339 mark the symbol as undefined. Note that the 10340 linker may attach linker created dynamic sections 10341 to the plugin bfd. Symbols defined in linker 10342 created sections are not plugin symbols. */ 10343 if (h->root.non_ir_ref 10344 && (h->root.type == bfd_link_hash_defined 10345 || h->root.type == bfd_link_hash_defweak) 10346 && (h->root.u.def.section->flags 10347 & SEC_LINKER_CREATED) == 0 10348 && h->root.u.def.section->owner != NULL 10349 && (h->root.u.def.section->owner->flags 10350 & BFD_PLUGIN) != 0) 10351 { 10352 h->root.type = bfd_link_hash_undefined; 10353 h->root.u.undef.abfd = h->root.u.def.section->owner; 10354 } 10355 10356 ps = NULL; 10357 if (h->root.type == bfd_link_hash_defined 10358 || h->root.type == bfd_link_hash_defweak) 10359 ps = &h->root.u.def.section; 10360 10361 sym_name = h->root.root.string; 10362 } 10363 else 10364 { 10365 Elf_Internal_Sym *sym = isymbuf + r_symndx; 10366 10367 s_type = ELF_ST_TYPE (sym->st_info); 10368 ps = &flinfo->sections[r_symndx]; 10369 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, 10370 sym, *ps); 10371 } 10372 10373 if ((s_type == STT_RELC || s_type == STT_SRELC) 10374 && !bfd_link_relocatable (flinfo->info)) 10375 { 10376 bfd_vma val; 10377 bfd_vma dot = (rel->r_offset 10378 + o->output_offset + o->output_section->vma); 10379#ifdef DEBUG 10380 printf ("Encountered a complex symbol!"); 10381 printf (" (input_bfd %s, section %s, reloc %ld\n", 10382 input_bfd->filename, o->name, 10383 (long) (rel - internal_relocs)); 10384 printf (" symbol: idx %8.8lx, name %s\n", 10385 r_symndx, sym_name); 10386 printf (" reloc : info %8.8lx, addr %8.8lx\n", 10387 (unsigned long) rel->r_info, 10388 (unsigned long) rel->r_offset); 10389#endif 10390 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, 10391 isymbuf, locsymcount, s_type == STT_SRELC)) 10392 return FALSE; 10393 10394 /* Symbol evaluated OK. Update to absolute value. */ 10395 set_symbol_value (input_bfd, isymbuf, locsymcount, 10396 r_symndx, val); 10397 continue; 10398 } 10399 10400 if (action_discarded != -1 && ps != NULL) 10401 { 10402 /* Complain if the definition comes from a 10403 discarded section. */ 10404 if ((sec = *ps) != NULL && discarded_section (sec)) 10405 { 10406 BFD_ASSERT (r_symndx != STN_UNDEF); 10407 if (action_discarded & COMPLAIN) 10408 (*flinfo->info->callbacks->einfo) 10409 /* xgettext:c-format */ 10410 (_("%X`%s' referenced in section `%A' of %B: " 10411 "defined in discarded section `%A' of %B\n"), 10412 sym_name, o, input_bfd, sec, sec->owner); 10413 10414 /* Try to do the best we can to support buggy old 10415 versions of gcc. Pretend that the symbol is 10416 really defined in the kept linkonce section. 10417 FIXME: This is quite broken. Modifying the 10418 symbol here means we will be changing all later 10419 uses of the symbol, not just in this section. */ 10420 if (action_discarded & PRETEND) 10421 { 10422 asection *kept; 10423 10424 kept = _bfd_elf_check_kept_section (sec, 10425 flinfo->info); 10426 if (kept != NULL) 10427 { 10428 *ps = kept; 10429 continue; 10430 } 10431 } 10432 } 10433 } 10434 } 10435 10436 /* Relocate the section by invoking a back end routine. 10437 10438 The back end routine is responsible for adjusting the 10439 section contents as necessary, and (if using Rela relocs 10440 and generating a relocatable output file) adjusting the 10441 reloc addend as necessary. 10442 10443 The back end routine does not have to worry about setting 10444 the reloc address or the reloc symbol index. 10445 10446 The back end routine is given a pointer to the swapped in 10447 internal symbols, and can access the hash table entries 10448 for the external symbols via elf_sym_hashes (input_bfd). 10449 10450 When generating relocatable output, the back end routine 10451 must handle STB_LOCAL/STT_SECTION symbols specially. The 10452 output symbol is going to be a section symbol 10453 corresponding to the output section, which will require 10454 the addend to be adjusted. */ 10455 10456 ret = (*relocate_section) (output_bfd, flinfo->info, 10457 input_bfd, o, contents, 10458 internal_relocs, 10459 isymbuf, 10460 flinfo->sections); 10461 if (!ret) 10462 return FALSE; 10463 10464 if (ret == 2 10465 || bfd_link_relocatable (flinfo->info) 10466 || flinfo->info->emitrelocations) 10467 { 10468 Elf_Internal_Rela *irela; 10469 Elf_Internal_Rela *irelaend, *irelamid; 10470 bfd_vma last_offset; 10471 struct elf_link_hash_entry **rel_hash; 10472 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; 10473 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; 10474 unsigned int next_erel; 10475 bfd_boolean rela_normal; 10476 struct bfd_elf_section_data *esdi, *esdo; 10477 10478 esdi = elf_section_data (o); 10479 esdo = elf_section_data (o->output_section); 10480 rela_normal = FALSE; 10481 10482 /* Adjust the reloc addresses and symbol indices. */ 10483 10484 irela = internal_relocs; 10485 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; 10486 rel_hash = esdo->rel.hashes + esdo->rel.count; 10487 /* We start processing the REL relocs, if any. When we reach 10488 IRELAMID in the loop, we switch to the RELA relocs. */ 10489 irelamid = irela; 10490 if (esdi->rel.hdr != NULL) 10491 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) 10492 * bed->s->int_rels_per_ext_rel); 10493 rel_hash_list = rel_hash; 10494 rela_hash_list = NULL; 10495 last_offset = o->output_offset; 10496 if (!bfd_link_relocatable (flinfo->info)) 10497 last_offset += o->output_section->vma; 10498 for (next_erel = 0; irela < irelaend; irela++, next_erel++) 10499 { 10500 unsigned long r_symndx; 10501 asection *sec; 10502 Elf_Internal_Sym sym; 10503 10504 if (next_erel == bed->s->int_rels_per_ext_rel) 10505 { 10506 rel_hash++; 10507 next_erel = 0; 10508 } 10509 10510 if (irela == irelamid) 10511 { 10512 rel_hash = esdo->rela.hashes + esdo->rela.count; 10513 rela_hash_list = rel_hash; 10514 rela_normal = bed->rela_normal; 10515 } 10516 10517 irela->r_offset = _bfd_elf_section_offset (output_bfd, 10518 flinfo->info, o, 10519 irela->r_offset); 10520 if (irela->r_offset >= (bfd_vma) -2) 10521 { 10522 /* This is a reloc for a deleted entry or somesuch. 10523 Turn it into an R_*_NONE reloc, at the same 10524 offset as the last reloc. elf_eh_frame.c and 10525 bfd_elf_discard_info rely on reloc offsets 10526 being ordered. */ 10527 irela->r_offset = last_offset; 10528 irela->r_info = 0; 10529 irela->r_addend = 0; 10530 continue; 10531 } 10532 10533 irela->r_offset += o->output_offset; 10534 10535 /* Relocs in an executable have to be virtual addresses. */ 10536 if (!bfd_link_relocatable (flinfo->info)) 10537 irela->r_offset += o->output_section->vma; 10538 10539 last_offset = irela->r_offset; 10540 10541 r_symndx = irela->r_info >> r_sym_shift; 10542 if (r_symndx == STN_UNDEF) 10543 continue; 10544 10545 if (r_symndx >= locsymcount 10546 || (elf_bad_symtab (input_bfd) 10547 && flinfo->sections[r_symndx] == NULL)) 10548 { 10549 struct elf_link_hash_entry *rh; 10550 unsigned long indx; 10551 10552 /* This is a reloc against a global symbol. We 10553 have not yet output all the local symbols, so 10554 we do not know the symbol index of any global 10555 symbol. We set the rel_hash entry for this 10556 reloc to point to the global hash table entry 10557 for this symbol. The symbol index is then 10558 set at the end of bfd_elf_final_link. */ 10559 indx = r_symndx - extsymoff; 10560 rh = elf_sym_hashes (input_bfd)[indx]; 10561 while (rh->root.type == bfd_link_hash_indirect 10562 || rh->root.type == bfd_link_hash_warning) 10563 rh = (struct elf_link_hash_entry *) rh->root.u.i.link; 10564 10565 /* Setting the index to -2 tells 10566 elf_link_output_extsym that this symbol is 10567 used by a reloc. */ 10568 BFD_ASSERT (rh->indx < 0); 10569 rh->indx = -2; 10570 10571 *rel_hash = rh; 10572 10573 continue; 10574 } 10575 10576 /* This is a reloc against a local symbol. */ 10577 10578 *rel_hash = NULL; 10579 sym = isymbuf[r_symndx]; 10580 sec = flinfo->sections[r_symndx]; 10581 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) 10582 { 10583 /* I suppose the backend ought to fill in the 10584 section of any STT_SECTION symbol against a 10585 processor specific section. */ 10586 r_symndx = STN_UNDEF; 10587 if (bfd_is_abs_section (sec)) 10588 ; 10589 else if (sec == NULL || sec->owner == NULL) 10590 { 10591 bfd_set_error (bfd_error_bad_value); 10592 return FALSE; 10593 } 10594 else 10595 { 10596 asection *osec = sec->output_section; 10597 10598 /* If we have discarded a section, the output 10599 section will be the absolute section. In 10600 case of discarded SEC_MERGE sections, use 10601 the kept section. relocate_section should 10602 have already handled discarded linkonce 10603 sections. */ 10604 if (bfd_is_abs_section (osec) 10605 && sec->kept_section != NULL 10606 && sec->kept_section->output_section != NULL) 10607 { 10608 osec = sec->kept_section->output_section; 10609 irela->r_addend -= osec->vma; 10610 } 10611 10612 if (!bfd_is_abs_section (osec)) 10613 { 10614 r_symndx = osec->target_index; 10615 if (r_symndx == STN_UNDEF) 10616 { 10617 irela->r_addend += osec->vma; 10618 osec = _bfd_nearby_section (output_bfd, osec, 10619 osec->vma); 10620 irela->r_addend -= osec->vma; 10621 r_symndx = osec->target_index; 10622 } 10623 } 10624 } 10625 10626 /* Adjust the addend according to where the 10627 section winds up in the output section. */ 10628 if (rela_normal) 10629 irela->r_addend += sec->output_offset; 10630 } 10631 else 10632 { 10633 if (flinfo->indices[r_symndx] == -1) 10634 { 10635 unsigned long shlink; 10636 const char *name; 10637 asection *osec; 10638 long indx; 10639 10640 if (flinfo->info->strip == strip_all) 10641 { 10642 /* You can't do ld -r -s. */ 10643 bfd_set_error (bfd_error_invalid_operation); 10644 return FALSE; 10645 } 10646 10647 /* This symbol was skipped earlier, but 10648 since it is needed by a reloc, we 10649 must output it now. */ 10650 shlink = symtab_hdr->sh_link; 10651 name = (bfd_elf_string_from_elf_section 10652 (input_bfd, shlink, sym.st_name)); 10653 if (name == NULL) 10654 return FALSE; 10655 10656 osec = sec->output_section; 10657 sym.st_shndx = 10658 _bfd_elf_section_from_bfd_section (output_bfd, 10659 osec); 10660 if (sym.st_shndx == SHN_BAD) 10661 return FALSE; 10662 10663 sym.st_value += sec->output_offset; 10664 if (!bfd_link_relocatable (flinfo->info)) 10665 { 10666 sym.st_value += osec->vma; 10667 if (ELF_ST_TYPE (sym.st_info) == STT_TLS) 10668 { 10669 /* STT_TLS symbols are relative to PT_TLS 10670 segment base. */ 10671 BFD_ASSERT (elf_hash_table (flinfo->info) 10672 ->tls_sec != NULL); 10673 sym.st_value -= (elf_hash_table (flinfo->info) 10674 ->tls_sec->vma); 10675 } 10676 } 10677 10678 indx = bfd_get_symcount (output_bfd); 10679 ret = elf_link_output_symstrtab (flinfo, name, 10680 &sym, sec, 10681 NULL); 10682 if (ret == 0) 10683 return FALSE; 10684 else if (ret == 1) 10685 flinfo->indices[r_symndx] = indx; 10686 else 10687 abort (); 10688 } 10689 10690 r_symndx = flinfo->indices[r_symndx]; 10691 } 10692 10693 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift 10694 | (irela->r_info & r_type_mask)); 10695 } 10696 10697 /* Swap out the relocs. */ 10698 input_rel_hdr = esdi->rel.hdr; 10699 if (input_rel_hdr && input_rel_hdr->sh_size != 0) 10700 { 10701 if (!bed->elf_backend_emit_relocs (output_bfd, o, 10702 input_rel_hdr, 10703 internal_relocs, 10704 rel_hash_list)) 10705 return FALSE; 10706 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) 10707 * bed->s->int_rels_per_ext_rel); 10708 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); 10709 } 10710 10711 input_rela_hdr = esdi->rela.hdr; 10712 if (input_rela_hdr && input_rela_hdr->sh_size != 0) 10713 { 10714 if (!bed->elf_backend_emit_relocs (output_bfd, o, 10715 input_rela_hdr, 10716 internal_relocs, 10717 rela_hash_list)) 10718 return FALSE; 10719 } 10720 } 10721 } 10722 10723 /* Write out the modified section contents. */ 10724 if (bed->elf_backend_write_section 10725 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, 10726 contents)) 10727 { 10728 /* Section written out. */ 10729 } 10730 else switch (o->sec_info_type) 10731 { 10732 case SEC_INFO_TYPE_STABS: 10733 if (! (_bfd_write_section_stabs 10734 (output_bfd, 10735 &elf_hash_table (flinfo->info)->stab_info, 10736 o, &elf_section_data (o)->sec_info, contents))) 10737 return FALSE; 10738 break; 10739 case SEC_INFO_TYPE_MERGE: 10740 if (! _bfd_write_merged_section (output_bfd, o, 10741 elf_section_data (o)->sec_info)) 10742 return FALSE; 10743 break; 10744 case SEC_INFO_TYPE_EH_FRAME: 10745 { 10746 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, 10747 o, contents)) 10748 return FALSE; 10749 } 10750 break; 10751 case SEC_INFO_TYPE_EH_FRAME_ENTRY: 10752 { 10753 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, 10754 flinfo->info, 10755 o, contents)) 10756 return FALSE; 10757 } 10758 break; 10759 default: 10760 { 10761 if (! (o->flags & SEC_EXCLUDE)) 10762 { 10763 file_ptr offset = (file_ptr) o->output_offset; 10764 bfd_size_type todo = o->size; 10765 10766 offset *= bfd_octets_per_byte (output_bfd); 10767 10768 if ((o->flags & SEC_ELF_REVERSE_COPY)) 10769 { 10770 /* Reverse-copy input section to output. */ 10771 do 10772 { 10773 todo -= address_size; 10774 if (! bfd_set_section_contents (output_bfd, 10775 o->output_section, 10776 contents + todo, 10777 offset, 10778 address_size)) 10779 return FALSE; 10780 if (todo == 0) 10781 break; 10782 offset += address_size; 10783 } 10784 while (1); 10785 } 10786 else if (! bfd_set_section_contents (output_bfd, 10787 o->output_section, 10788 contents, 10789 offset, todo)) 10790 return FALSE; 10791 } 10792 } 10793 break; 10794 } 10795 } 10796 10797 return TRUE; 10798} 10799 10800/* Generate a reloc when linking an ELF file. This is a reloc 10801 requested by the linker, and does not come from any input file. This 10802 is used to build constructor and destructor tables when linking 10803 with -Ur. */ 10804 10805static bfd_boolean 10806elf_reloc_link_order (bfd *output_bfd, 10807 struct bfd_link_info *info, 10808 asection *output_section, 10809 struct bfd_link_order *link_order) 10810{ 10811 reloc_howto_type *howto; 10812 long indx; 10813 bfd_vma offset; 10814 bfd_vma addend; 10815 struct bfd_elf_section_reloc_data *reldata; 10816 struct elf_link_hash_entry **rel_hash_ptr; 10817 Elf_Internal_Shdr *rel_hdr; 10818 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 10819 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; 10820 bfd_byte *erel; 10821 unsigned int i; 10822 struct bfd_elf_section_data *esdo = elf_section_data (output_section); 10823 10824 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); 10825 if (howto == NULL) 10826 { 10827 bfd_set_error (bfd_error_bad_value); 10828 return FALSE; 10829 } 10830 10831 addend = link_order->u.reloc.p->addend; 10832 10833 if (esdo->rel.hdr) 10834 reldata = &esdo->rel; 10835 else if (esdo->rela.hdr) 10836 reldata = &esdo->rela; 10837 else 10838 { 10839 reldata = NULL; 10840 BFD_ASSERT (0); 10841 } 10842 10843 /* Figure out the symbol index. */ 10844 rel_hash_ptr = reldata->hashes + reldata->count; 10845 if (link_order->type == bfd_section_reloc_link_order) 10846 { 10847 indx = link_order->u.reloc.p->u.section->target_index; 10848 BFD_ASSERT (indx != 0); 10849 *rel_hash_ptr = NULL; 10850 } 10851 else 10852 { 10853 struct elf_link_hash_entry *h; 10854 10855 /* Treat a reloc against a defined symbol as though it were 10856 actually against the section. */ 10857 h = ((struct elf_link_hash_entry *) 10858 bfd_wrapped_link_hash_lookup (output_bfd, info, 10859 link_order->u.reloc.p->u.name, 10860 FALSE, FALSE, TRUE)); 10861 if (h != NULL 10862 && (h->root.type == bfd_link_hash_defined 10863 || h->root.type == bfd_link_hash_defweak)) 10864 { 10865 asection *section; 10866 10867 section = h->root.u.def.section; 10868 indx = section->output_section->target_index; 10869 *rel_hash_ptr = NULL; 10870 /* It seems that we ought to add the symbol value to the 10871 addend here, but in practice it has already been added 10872 because it was passed to constructor_callback. */ 10873 addend += section->output_section->vma + section->output_offset; 10874 } 10875 else if (h != NULL) 10876 { 10877 /* Setting the index to -2 tells elf_link_output_extsym that 10878 this symbol is used by a reloc. */ 10879 h->indx = -2; 10880 *rel_hash_ptr = h; 10881 indx = 0; 10882 } 10883 else 10884 { 10885 (*info->callbacks->unattached_reloc) 10886 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); 10887 indx = 0; 10888 } 10889 } 10890 10891 /* If this is an inplace reloc, we must write the addend into the 10892 object file. */ 10893 if (howto->partial_inplace && addend != 0) 10894 { 10895 bfd_size_type size; 10896 bfd_reloc_status_type rstat; 10897 bfd_byte *buf; 10898 bfd_boolean ok; 10899 const char *sym_name; 10900 10901 size = (bfd_size_type) bfd_get_reloc_size (howto); 10902 buf = (bfd_byte *) bfd_zmalloc (size); 10903 if (buf == NULL && size != 0) 10904 return FALSE; 10905 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); 10906 switch (rstat) 10907 { 10908 case bfd_reloc_ok: 10909 break; 10910 10911 default: 10912 case bfd_reloc_outofrange: 10913 abort (); 10914 10915 case bfd_reloc_overflow: 10916 if (link_order->type == bfd_section_reloc_link_order) 10917 sym_name = bfd_section_name (output_bfd, 10918 link_order->u.reloc.p->u.section); 10919 else 10920 sym_name = link_order->u.reloc.p->u.name; 10921 (*info->callbacks->reloc_overflow) (info, NULL, sym_name, 10922 howto->name, addend, NULL, NULL, 10923 (bfd_vma) 0); 10924 break; 10925 } 10926 10927 ok = bfd_set_section_contents (output_bfd, output_section, buf, 10928 link_order->offset 10929 * bfd_octets_per_byte (output_bfd), 10930 size); 10931 free (buf); 10932 if (! ok) 10933 return FALSE; 10934 } 10935 10936 /* The address of a reloc is relative to the section in a 10937 relocatable file, and is a virtual address in an executable 10938 file. */ 10939 offset = link_order->offset; 10940 if (! bfd_link_relocatable (info)) 10941 offset += output_section->vma; 10942 10943 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) 10944 { 10945 irel[i].r_offset = offset; 10946 irel[i].r_info = 0; 10947 irel[i].r_addend = 0; 10948 } 10949 if (bed->s->arch_size == 32) 10950 irel[0].r_info = ELF32_R_INFO (indx, howto->type); 10951 else 10952 irel[0].r_info = ELF64_R_INFO (indx, howto->type); 10953 10954 rel_hdr = reldata->hdr; 10955 erel = rel_hdr->contents; 10956 if (rel_hdr->sh_type == SHT_REL) 10957 { 10958 erel += reldata->count * bed->s->sizeof_rel; 10959 (*bed->s->swap_reloc_out) (output_bfd, irel, erel); 10960 } 10961 else 10962 { 10963 irel[0].r_addend = addend; 10964 erel += reldata->count * bed->s->sizeof_rela; 10965 (*bed->s->swap_reloca_out) (output_bfd, irel, erel); 10966 } 10967 10968 ++reldata->count; 10969 10970 return TRUE; 10971} 10972 10973 10974/* Get the output vma of the section pointed to by the sh_link field. */ 10975 10976static bfd_vma 10977elf_get_linked_section_vma (struct bfd_link_order *p) 10978{ 10979 Elf_Internal_Shdr **elf_shdrp; 10980 asection *s; 10981 int elfsec; 10982 10983 s = p->u.indirect.section; 10984 elf_shdrp = elf_elfsections (s->owner); 10985 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); 10986 elfsec = elf_shdrp[elfsec]->sh_link; 10987 /* PR 290: 10988 The Intel C compiler generates SHT_IA_64_UNWIND with 10989 SHF_LINK_ORDER. But it doesn't set the sh_link or 10990 sh_info fields. Hence we could get the situation 10991 where elfsec is 0. */ 10992 if (elfsec == 0) 10993 { 10994 const struct elf_backend_data *bed 10995 = get_elf_backend_data (s->owner); 10996 if (bed->link_order_error_handler) 10997 bed->link_order_error_handler 10998 /* xgettext:c-format */ 10999 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); 11000 return 0; 11001 } 11002 else 11003 { 11004 s = elf_shdrp[elfsec]->bfd_section; 11005 return s->output_section->vma + s->output_offset; 11006 } 11007} 11008 11009 11010/* Compare two sections based on the locations of the sections they are 11011 linked to. Used by elf_fixup_link_order. */ 11012 11013static int 11014compare_link_order (const void * a, const void * b) 11015{ 11016 bfd_vma apos; 11017 bfd_vma bpos; 11018 11019 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); 11020 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); 11021 if (apos < bpos) 11022 return -1; 11023 return apos > bpos; 11024} 11025 11026 11027/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same 11028 order as their linked sections. Returns false if this could not be done 11029 because an output section includes both ordered and unordered 11030 sections. Ideally we'd do this in the linker proper. */ 11031 11032static bfd_boolean 11033elf_fixup_link_order (bfd *abfd, asection *o) 11034{ 11035 int seen_linkorder; 11036 int seen_other; 11037 int n; 11038 struct bfd_link_order *p; 11039 bfd *sub; 11040 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11041 unsigned elfsec; 11042 struct bfd_link_order **sections; 11043 asection *s, *other_sec, *linkorder_sec; 11044 bfd_vma offset; 11045 11046 other_sec = NULL; 11047 linkorder_sec = NULL; 11048 seen_other = 0; 11049 seen_linkorder = 0; 11050 for (p = o->map_head.link_order; p != NULL; p = p->next) 11051 { 11052 if (p->type == bfd_indirect_link_order) 11053 { 11054 s = p->u.indirect.section; 11055 sub = s->owner; 11056 if (bfd_get_flavour (sub) == bfd_target_elf_flavour 11057 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass 11058 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) 11059 && elfsec < elf_numsections (sub) 11060 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER 11061 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) 11062 { 11063 seen_linkorder++; 11064 linkorder_sec = s; 11065 } 11066 else 11067 { 11068 seen_other++; 11069 other_sec = s; 11070 } 11071 } 11072 else 11073 seen_other++; 11074 11075 if (seen_other && seen_linkorder) 11076 { 11077 if (other_sec && linkorder_sec) 11078 _bfd_error_handler 11079 /* xgettext:c-format */ 11080 (_("%A has both ordered [`%A' in %B] " 11081 "and unordered [`%A' in %B] sections"), 11082 o, linkorder_sec, 11083 linkorder_sec->owner, other_sec, 11084 other_sec->owner); 11085 else 11086 _bfd_error_handler 11087 (_("%A has both ordered and unordered sections"), o); 11088 bfd_set_error (bfd_error_bad_value); 11089 return FALSE; 11090 } 11091 } 11092 11093 if (!seen_linkorder) 11094 return TRUE; 11095 11096 sections = (struct bfd_link_order **) 11097 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); 11098 if (sections == NULL) 11099 return FALSE; 11100 seen_linkorder = 0; 11101 11102 for (p = o->map_head.link_order; p != NULL; p = p->next) 11103 { 11104 sections[seen_linkorder++] = p; 11105 } 11106 /* Sort the input sections in the order of their linked section. */ 11107 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), 11108 compare_link_order); 11109 11110 /* Change the offsets of the sections. */ 11111 offset = 0; 11112 for (n = 0; n < seen_linkorder; n++) 11113 { 11114 s = sections[n]->u.indirect.section; 11115 offset &= ~(bfd_vma) 0 << s->alignment_power; 11116 s->output_offset = offset / bfd_octets_per_byte (abfd); 11117 sections[n]->offset = offset; 11118 offset += sections[n]->size; 11119 } 11120 11121 free (sections); 11122 return TRUE; 11123} 11124 11125/* Generate an import library in INFO->implib_bfd from symbols in ABFD. 11126 Returns TRUE upon success, FALSE otherwise. */ 11127 11128static bfd_boolean 11129elf_output_implib (bfd *abfd, struct bfd_link_info *info) 11130{ 11131 bfd_boolean ret = FALSE; 11132 bfd *implib_bfd; 11133 const struct elf_backend_data *bed; 11134 flagword flags; 11135 enum bfd_architecture arch; 11136 unsigned int mach; 11137 asymbol **sympp = NULL; 11138 long symsize; 11139 long symcount; 11140 long src_count; 11141 elf_symbol_type *osymbuf; 11142 11143 implib_bfd = info->out_implib_bfd; 11144 bed = get_elf_backend_data (abfd); 11145 11146 if (!bfd_set_format (implib_bfd, bfd_object)) 11147 return FALSE; 11148 11149 /* Use flag from executable but make it a relocatable object. */ 11150 flags = bfd_get_file_flags (abfd); 11151 flags &= ~HAS_RELOC; 11152 if (!bfd_set_start_address (implib_bfd, 0) 11153 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P)) 11154 return FALSE; 11155 11156 /* Copy architecture of output file to import library file. */ 11157 arch = bfd_get_arch (abfd); 11158 mach = bfd_get_mach (abfd); 11159 if (!bfd_set_arch_mach (implib_bfd, arch, mach) 11160 && (abfd->target_defaulted 11161 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) 11162 return FALSE; 11163 11164 /* Get symbol table size. */ 11165 symsize = bfd_get_symtab_upper_bound (abfd); 11166 if (symsize < 0) 11167 return FALSE; 11168 11169 /* Read in the symbol table. */ 11170 sympp = (asymbol **) xmalloc (symsize); 11171 symcount = bfd_canonicalize_symtab (abfd, sympp); 11172 if (symcount < 0) 11173 goto free_sym_buf; 11174 11175 /* Allow the BFD backend to copy any private header data it 11176 understands from the output BFD to the import library BFD. */ 11177 if (! bfd_copy_private_header_data (abfd, implib_bfd)) 11178 goto free_sym_buf; 11179 11180 /* Filter symbols to appear in the import library. */ 11181 if (bed->elf_backend_filter_implib_symbols) 11182 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, 11183 symcount); 11184 else 11185 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); 11186 if (symcount == 0) 11187 { 11188 bfd_set_error (bfd_error_no_symbols); 11189 _bfd_error_handler (_("%B: no symbol found for import library"), 11190 implib_bfd); 11191 goto free_sym_buf; 11192 } 11193 11194 11195 /* Make symbols absolute. */ 11196 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount, 11197 sizeof (*osymbuf)); 11198 for (src_count = 0; src_count < symcount; src_count++) 11199 { 11200 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], 11201 sizeof (*osymbuf)); 11202 osymbuf[src_count].symbol.section = bfd_abs_section_ptr; 11203 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; 11204 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; 11205 osymbuf[src_count].internal_elf_sym.st_value = 11206 osymbuf[src_count].symbol.value; 11207 sympp[src_count] = &osymbuf[src_count].symbol; 11208 } 11209 11210 bfd_set_symtab (implib_bfd, sympp, symcount); 11211 11212 /* Allow the BFD backend to copy any private data it understands 11213 from the output BFD to the import library BFD. This is done last 11214 to permit the routine to look at the filtered symbol table. */ 11215 if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) 11216 goto free_sym_buf; 11217 11218 if (!bfd_close (implib_bfd)) 11219 goto free_sym_buf; 11220 11221 ret = TRUE; 11222 11223free_sym_buf: 11224 free (sympp); 11225 return ret; 11226} 11227 11228static void 11229elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) 11230{ 11231 asection *o; 11232 11233 if (flinfo->symstrtab != NULL) 11234 _bfd_elf_strtab_free (flinfo->symstrtab); 11235 if (flinfo->contents != NULL) 11236 free (flinfo->contents); 11237 if (flinfo->external_relocs != NULL) 11238 free (flinfo->external_relocs); 11239 if (flinfo->internal_relocs != NULL) 11240 free (flinfo->internal_relocs); 11241 if (flinfo->external_syms != NULL) 11242 free (flinfo->external_syms); 11243 if (flinfo->locsym_shndx != NULL) 11244 free (flinfo->locsym_shndx); 11245 if (flinfo->internal_syms != NULL) 11246 free (flinfo->internal_syms); 11247 if (flinfo->indices != NULL) 11248 free (flinfo->indices); 11249 if (flinfo->sections != NULL) 11250 free (flinfo->sections); 11251 if (flinfo->symshndxbuf != NULL) 11252 free (flinfo->symshndxbuf); 11253 for (o = obfd->sections; o != NULL; o = o->next) 11254 { 11255 struct bfd_elf_section_data *esdo = elf_section_data (o); 11256 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) 11257 free (esdo->rel.hashes); 11258 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) 11259 free (esdo->rela.hashes); 11260 } 11261} 11262 11263/* Do the final step of an ELF link. */ 11264 11265bfd_boolean 11266bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) 11267{ 11268 bfd_boolean dynamic; 11269 bfd_boolean emit_relocs; 11270 bfd *dynobj; 11271 struct elf_final_link_info flinfo; 11272 asection *o; 11273 struct bfd_link_order *p; 11274 bfd *sub; 11275 bfd_size_type max_contents_size; 11276 bfd_size_type max_external_reloc_size; 11277 bfd_size_type max_internal_reloc_count; 11278 bfd_size_type max_sym_count; 11279 bfd_size_type max_sym_shndx_count; 11280 Elf_Internal_Sym elfsym; 11281 unsigned int i; 11282 Elf_Internal_Shdr *symtab_hdr; 11283 Elf_Internal_Shdr *symtab_shndx_hdr; 11284 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11285 struct elf_outext_info eoinfo; 11286 bfd_boolean merged; 11287 size_t relativecount = 0; 11288 asection *reldyn = 0; 11289 bfd_size_type amt; 11290 asection *attr_section = NULL; 11291 bfd_vma attr_size = 0; 11292 const char *std_attrs_section; 11293 struct elf_link_hash_table *htab = elf_hash_table (info); 11294 11295 if (!is_elf_hash_table (htab)) 11296 return FALSE; 11297 11298 if (bfd_link_pic (info)) 11299 abfd->flags |= DYNAMIC; 11300 11301 dynamic = htab->dynamic_sections_created; 11302 dynobj = htab->dynobj; 11303 11304 emit_relocs = (bfd_link_relocatable (info) 11305 || info->emitrelocations); 11306 11307 flinfo.info = info; 11308 flinfo.output_bfd = abfd; 11309 flinfo.symstrtab = _bfd_elf_strtab_init (); 11310 if (flinfo.symstrtab == NULL) 11311 return FALSE; 11312 11313 if (! dynamic) 11314 { 11315 flinfo.hash_sec = NULL; 11316 flinfo.symver_sec = NULL; 11317 } 11318 else 11319 { 11320 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); 11321 /* Note that dynsym_sec can be NULL (on VMS). */ 11322 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); 11323 /* Note that it is OK if symver_sec is NULL. */ 11324 } 11325 11326 flinfo.contents = NULL; 11327 flinfo.external_relocs = NULL; 11328 flinfo.internal_relocs = NULL; 11329 flinfo.external_syms = NULL; 11330 flinfo.locsym_shndx = NULL; 11331 flinfo.internal_syms = NULL; 11332 flinfo.indices = NULL; 11333 flinfo.sections = NULL; 11334 flinfo.symshndxbuf = NULL; 11335 flinfo.filesym_count = 0; 11336 11337 /* The object attributes have been merged. Remove the input 11338 sections from the link, and set the contents of the output 11339 secton. */ 11340 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; 11341 for (o = abfd->sections; o != NULL; o = o->next) 11342 { 11343 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) 11344 || strcmp (o->name, ".gnu.attributes") == 0) 11345 { 11346 for (p = o->map_head.link_order; p != NULL; p = p->next) 11347 { 11348 asection *input_section; 11349 11350 if (p->type != bfd_indirect_link_order) 11351 continue; 11352 input_section = p->u.indirect.section; 11353 /* Hack: reset the SEC_HAS_CONTENTS flag so that 11354 elf_link_input_bfd ignores this section. */ 11355 input_section->flags &= ~SEC_HAS_CONTENTS; 11356 } 11357 11358 attr_size = bfd_elf_obj_attr_size (abfd); 11359 if (attr_size) 11360 { 11361 bfd_set_section_size (abfd, o, attr_size); 11362 attr_section = o; 11363 /* Skip this section later on. */ 11364 o->map_head.link_order = NULL; 11365 } 11366 else 11367 o->flags |= SEC_EXCLUDE; 11368 } 11369 } 11370 11371 /* Count up the number of relocations we will output for each output 11372 section, so that we know the sizes of the reloc sections. We 11373 also figure out some maximum sizes. */ 11374 max_contents_size = 0; 11375 max_external_reloc_size = 0; 11376 max_internal_reloc_count = 0; 11377 max_sym_count = 0; 11378 max_sym_shndx_count = 0; 11379 merged = FALSE; 11380 for (o = abfd->sections; o != NULL; o = o->next) 11381 { 11382 struct bfd_elf_section_data *esdo = elf_section_data (o); 11383 o->reloc_count = 0; 11384 11385 for (p = o->map_head.link_order; p != NULL; p = p->next) 11386 { 11387 unsigned int reloc_count = 0; 11388 unsigned int additional_reloc_count = 0; 11389 struct bfd_elf_section_data *esdi = NULL; 11390 11391 if (p->type == bfd_section_reloc_link_order 11392 || p->type == bfd_symbol_reloc_link_order) 11393 reloc_count = 1; 11394 else if (p->type == bfd_indirect_link_order) 11395 { 11396 asection *sec; 11397 11398 sec = p->u.indirect.section; 11399 11400 /* Mark all sections which are to be included in the 11401 link. This will normally be every section. We need 11402 to do this so that we can identify any sections which 11403 the linker has decided to not include. */ 11404 sec->linker_mark = TRUE; 11405 11406 if (sec->flags & SEC_MERGE) 11407 merged = TRUE; 11408 11409 if (sec->rawsize > max_contents_size) 11410 max_contents_size = sec->rawsize; 11411 if (sec->size > max_contents_size) 11412 max_contents_size = sec->size; 11413 11414 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour 11415 && (sec->owner->flags & DYNAMIC) == 0) 11416 { 11417 size_t sym_count; 11418 11419 /* We are interested in just local symbols, not all 11420 symbols. */ 11421 if (elf_bad_symtab (sec->owner)) 11422 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size 11423 / bed->s->sizeof_sym); 11424 else 11425 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; 11426 11427 if (sym_count > max_sym_count) 11428 max_sym_count = sym_count; 11429 11430 if (sym_count > max_sym_shndx_count 11431 && elf_symtab_shndx_list (sec->owner) != NULL) 11432 max_sym_shndx_count = sym_count; 11433 11434 if (esdo->this_hdr.sh_type == SHT_REL 11435 || esdo->this_hdr.sh_type == SHT_RELA) 11436 /* Some backends use reloc_count in relocation sections 11437 to count particular types of relocs. Of course, 11438 reloc sections themselves can't have relocations. */ 11439 ; 11440 else if (emit_relocs) 11441 { 11442 reloc_count = sec->reloc_count; 11443 if (bed->elf_backend_count_additional_relocs) 11444 { 11445 int c; 11446 c = (*bed->elf_backend_count_additional_relocs) (sec); 11447 additional_reloc_count += c; 11448 } 11449 } 11450 else if (bed->elf_backend_count_relocs) 11451 reloc_count = (*bed->elf_backend_count_relocs) (info, sec); 11452 11453 esdi = elf_section_data (sec); 11454 11455 if ((sec->flags & SEC_RELOC) != 0) 11456 { 11457 size_t ext_size = 0; 11458 11459 if (esdi->rel.hdr != NULL) 11460 ext_size = esdi->rel.hdr->sh_size; 11461 if (esdi->rela.hdr != NULL) 11462 ext_size += esdi->rela.hdr->sh_size; 11463 11464 if (ext_size > max_external_reloc_size) 11465 max_external_reloc_size = ext_size; 11466 if (sec->reloc_count > max_internal_reloc_count) 11467 max_internal_reloc_count = sec->reloc_count; 11468 } 11469 } 11470 } 11471 11472 if (reloc_count == 0) 11473 continue; 11474 11475 reloc_count += additional_reloc_count; 11476 o->reloc_count += reloc_count; 11477 11478 if (p->type == bfd_indirect_link_order && emit_relocs) 11479 { 11480 if (esdi->rel.hdr) 11481 { 11482 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); 11483 esdo->rel.count += additional_reloc_count; 11484 } 11485 if (esdi->rela.hdr) 11486 { 11487 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); 11488 esdo->rela.count += additional_reloc_count; 11489 } 11490 } 11491 else 11492 { 11493 if (o->use_rela_p) 11494 esdo->rela.count += reloc_count; 11495 else 11496 esdo->rel.count += reloc_count; 11497 } 11498 } 11499 11500 if (o->reloc_count > 0) 11501 o->flags |= SEC_RELOC; 11502 else 11503 { 11504 /* Explicitly clear the SEC_RELOC flag. The linker tends to 11505 set it (this is probably a bug) and if it is set 11506 assign_section_numbers will create a reloc section. */ 11507 o->flags &=~ SEC_RELOC; 11508 } 11509 11510 /* If the SEC_ALLOC flag is not set, force the section VMA to 11511 zero. This is done in elf_fake_sections as well, but forcing 11512 the VMA to 0 here will ensure that relocs against these 11513 sections are handled correctly. */ 11514 if ((o->flags & SEC_ALLOC) == 0 11515 && ! o->user_set_vma) 11516 o->vma = 0; 11517 } 11518 11519 if (! bfd_link_relocatable (info) && merged) 11520 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); 11521 11522 /* Figure out the file positions for everything but the symbol table 11523 and the relocs. We set symcount to force assign_section_numbers 11524 to create a symbol table. */ 11525 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; 11526 BFD_ASSERT (! abfd->output_has_begun); 11527 if (! _bfd_elf_compute_section_file_positions (abfd, info)) 11528 goto error_return; 11529 11530 /* Set sizes, and assign file positions for reloc sections. */ 11531 for (o = abfd->sections; o != NULL; o = o->next) 11532 { 11533 struct bfd_elf_section_data *esdo = elf_section_data (o); 11534 if ((o->flags & SEC_RELOC) != 0) 11535 { 11536 if (esdo->rel.hdr 11537 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) 11538 goto error_return; 11539 11540 if (esdo->rela.hdr 11541 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) 11542 goto error_return; 11543 } 11544 11545 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them 11546 to count upwards while actually outputting the relocations. */ 11547 esdo->rel.count = 0; 11548 esdo->rela.count = 0; 11549 11550 if (esdo->this_hdr.sh_offset == (file_ptr) -1) 11551 { 11552 /* Cache the section contents so that they can be compressed 11553 later. Use bfd_malloc since it will be freed by 11554 bfd_compress_section_contents. */ 11555 unsigned char *contents = esdo->this_hdr.contents; 11556 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) 11557 abort (); 11558 contents 11559 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); 11560 if (contents == NULL) 11561 goto error_return; 11562 esdo->this_hdr.contents = contents; 11563 } 11564 } 11565 11566 /* We have now assigned file positions for all the sections except 11567 .symtab, .strtab, and non-loaded reloc sections. We start the 11568 .symtab section at the current file position, and write directly 11569 to it. We build the .strtab section in memory. */ 11570 bfd_get_symcount (abfd) = 0; 11571 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 11572 /* sh_name is set in prep_headers. */ 11573 symtab_hdr->sh_type = SHT_SYMTAB; 11574 /* sh_flags, sh_addr and sh_size all start off zero. */ 11575 symtab_hdr->sh_entsize = bed->s->sizeof_sym; 11576 /* sh_link is set in assign_section_numbers. */ 11577 /* sh_info is set below. */ 11578 /* sh_offset is set just below. */ 11579 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 11580 11581 if (max_sym_count < 20) 11582 max_sym_count = 20; 11583 htab->strtabsize = max_sym_count; 11584 amt = max_sym_count * sizeof (struct elf_sym_strtab); 11585 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); 11586 if (htab->strtab == NULL) 11587 goto error_return; 11588 /* The real buffer will be allocated in elf_link_swap_symbols_out. */ 11589 flinfo.symshndxbuf 11590 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) 11591 ? (Elf_External_Sym_Shndx *) -1 : NULL); 11592 11593 if (info->strip != strip_all || emit_relocs) 11594 { 11595 file_ptr off = elf_next_file_pos (abfd); 11596 11597 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); 11598 11599 /* Note that at this point elf_next_file_pos (abfd) is 11600 incorrect. We do not yet know the size of the .symtab section. 11601 We correct next_file_pos below, after we do know the size. */ 11602 11603 /* Start writing out the symbol table. The first symbol is always a 11604 dummy symbol. */ 11605 elfsym.st_value = 0; 11606 elfsym.st_size = 0; 11607 elfsym.st_info = 0; 11608 elfsym.st_other = 0; 11609 elfsym.st_shndx = SHN_UNDEF; 11610 elfsym.st_target_internal = 0; 11611 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, 11612 bfd_und_section_ptr, NULL) != 1) 11613 goto error_return; 11614 11615 /* Output a symbol for each section. We output these even if we are 11616 discarding local symbols, since they are used for relocs. These 11617 symbols have no names. We store the index of each one in the 11618 index field of the section, so that we can find it again when 11619 outputting relocs. */ 11620 11621 elfsym.st_size = 0; 11622 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 11623 elfsym.st_other = 0; 11624 elfsym.st_value = 0; 11625 elfsym.st_target_internal = 0; 11626 for (i = 1; i < elf_numsections (abfd); i++) 11627 { 11628 o = bfd_section_from_elf_index (abfd, i); 11629 if (o != NULL) 11630 { 11631 o->target_index = bfd_get_symcount (abfd); 11632 elfsym.st_shndx = i; 11633 if (!bfd_link_relocatable (info)) 11634 elfsym.st_value = o->vma; 11635 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, 11636 NULL) != 1) 11637 goto error_return; 11638 } 11639 } 11640 } 11641 11642 /* Allocate some memory to hold information read in from the input 11643 files. */ 11644 if (max_contents_size != 0) 11645 { 11646 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); 11647 if (flinfo.contents == NULL) 11648 goto error_return; 11649 } 11650 11651 if (max_external_reloc_size != 0) 11652 { 11653 flinfo.external_relocs = bfd_malloc (max_external_reloc_size); 11654 if (flinfo.external_relocs == NULL) 11655 goto error_return; 11656 } 11657 11658 if (max_internal_reloc_count != 0) 11659 { 11660 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; 11661 amt *= sizeof (Elf_Internal_Rela); 11662 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); 11663 if (flinfo.internal_relocs == NULL) 11664 goto error_return; 11665 } 11666 11667 if (max_sym_count != 0) 11668 { 11669 amt = max_sym_count * bed->s->sizeof_sym; 11670 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); 11671 if (flinfo.external_syms == NULL) 11672 goto error_return; 11673 11674 amt = max_sym_count * sizeof (Elf_Internal_Sym); 11675 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); 11676 if (flinfo.internal_syms == NULL) 11677 goto error_return; 11678 11679 amt = max_sym_count * sizeof (long); 11680 flinfo.indices = (long int *) bfd_malloc (amt); 11681 if (flinfo.indices == NULL) 11682 goto error_return; 11683 11684 amt = max_sym_count * sizeof (asection *); 11685 flinfo.sections = (asection **) bfd_malloc (amt); 11686 if (flinfo.sections == NULL) 11687 goto error_return; 11688 } 11689 11690 if (max_sym_shndx_count != 0) 11691 { 11692 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); 11693 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); 11694 if (flinfo.locsym_shndx == NULL) 11695 goto error_return; 11696 } 11697 11698 if (htab->tls_sec) 11699 { 11700 bfd_vma base, end = 0; 11701 asection *sec; 11702 11703 for (sec = htab->tls_sec; 11704 sec && (sec->flags & SEC_THREAD_LOCAL); 11705 sec = sec->next) 11706 { 11707 bfd_size_type size = sec->size; 11708 11709 if (size == 0 11710 && (sec->flags & SEC_HAS_CONTENTS) == 0) 11711 { 11712 struct bfd_link_order *ord = sec->map_tail.link_order; 11713 11714 if (ord != NULL) 11715 size = ord->offset + ord->size; 11716 } 11717 end = sec->vma + size; 11718 } 11719 base = htab->tls_sec->vma; 11720 /* Only align end of TLS section if static TLS doesn't have special 11721 alignment requirements. */ 11722 if (bed->static_tls_alignment == 1) 11723 end = align_power (end, htab->tls_sec->alignment_power); 11724 htab->tls_size = end - base; 11725 } 11726 11727 /* Reorder SHF_LINK_ORDER sections. */ 11728 for (o = abfd->sections; o != NULL; o = o->next) 11729 { 11730 if (!elf_fixup_link_order (abfd, o)) 11731 return FALSE; 11732 } 11733 11734 if (!_bfd_elf_fixup_eh_frame_hdr (info)) 11735 return FALSE; 11736 11737 /* Since ELF permits relocations to be against local symbols, we 11738 must have the local symbols available when we do the relocations. 11739 Since we would rather only read the local symbols once, and we 11740 would rather not keep them in memory, we handle all the 11741 relocations for a single input file at the same time. 11742 11743 Unfortunately, there is no way to know the total number of local 11744 symbols until we have seen all of them, and the local symbol 11745 indices precede the global symbol indices. This means that when 11746 we are generating relocatable output, and we see a reloc against 11747 a global symbol, we can not know the symbol index until we have 11748 finished examining all the local symbols to see which ones we are 11749 going to output. To deal with this, we keep the relocations in 11750 memory, and don't output them until the end of the link. This is 11751 an unfortunate waste of memory, but I don't see a good way around 11752 it. Fortunately, it only happens when performing a relocatable 11753 link, which is not the common case. FIXME: If keep_memory is set 11754 we could write the relocs out and then read them again; I don't 11755 know how bad the memory loss will be. */ 11756 11757 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 11758 sub->output_has_begun = FALSE; 11759 for (o = abfd->sections; o != NULL; o = o->next) 11760 { 11761 for (p = o->map_head.link_order; p != NULL; p = p->next) 11762 { 11763 if (p->type == bfd_indirect_link_order 11764 && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) 11765 == bfd_target_elf_flavour) 11766 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) 11767 { 11768 if (! sub->output_has_begun) 11769 { 11770 if (! elf_link_input_bfd (&flinfo, sub)) 11771 goto error_return; 11772 sub->output_has_begun = TRUE; 11773 } 11774 } 11775 else if (p->type == bfd_section_reloc_link_order 11776 || p->type == bfd_symbol_reloc_link_order) 11777 { 11778 if (! elf_reloc_link_order (abfd, info, o, p)) 11779 goto error_return; 11780 } 11781 else 11782 { 11783 if (! _bfd_default_link_order (abfd, info, o, p)) 11784 { 11785 if (p->type == bfd_indirect_link_order 11786 && (bfd_get_flavour (sub) 11787 == bfd_target_elf_flavour) 11788 && (elf_elfheader (sub)->e_ident[EI_CLASS] 11789 != bed->s->elfclass)) 11790 { 11791 const char *iclass, *oclass; 11792 11793 switch (bed->s->elfclass) 11794 { 11795 case ELFCLASS64: oclass = "ELFCLASS64"; break; 11796 case ELFCLASS32: oclass = "ELFCLASS32"; break; 11797 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; 11798 default: abort (); 11799 } 11800 11801 switch (elf_elfheader (sub)->e_ident[EI_CLASS]) 11802 { 11803 case ELFCLASS64: iclass = "ELFCLASS64"; break; 11804 case ELFCLASS32: iclass = "ELFCLASS32"; break; 11805 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; 11806 default: abort (); 11807 } 11808 11809 bfd_set_error (bfd_error_wrong_format); 11810 _bfd_error_handler 11811 /* xgettext:c-format */ 11812 (_("%B: file class %s incompatible with %s"), 11813 sub, iclass, oclass); 11814 } 11815 11816 goto error_return; 11817 } 11818 } 11819 } 11820 } 11821 11822 /* Free symbol buffer if needed. */ 11823 if (!info->reduce_memory_overheads) 11824 { 11825 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 11826 if (bfd_get_flavour (sub) == bfd_target_elf_flavour 11827 && elf_tdata (sub)->symbuf) 11828 { 11829 free (elf_tdata (sub)->symbuf); 11830 elf_tdata (sub)->symbuf = NULL; 11831 } 11832 } 11833 11834 /* Output any global symbols that got converted to local in a 11835 version script or due to symbol visibility. We do this in a 11836 separate step since ELF requires all local symbols to appear 11837 prior to any global symbols. FIXME: We should only do this if 11838 some global symbols were, in fact, converted to become local. 11839 FIXME: Will this work correctly with the Irix 5 linker? */ 11840 eoinfo.failed = FALSE; 11841 eoinfo.flinfo = &flinfo; 11842 eoinfo.localsyms = TRUE; 11843 eoinfo.file_sym_done = FALSE; 11844 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); 11845 if (eoinfo.failed) 11846 return FALSE; 11847 11848 /* If backend needs to output some local symbols not present in the hash 11849 table, do it now. */ 11850 if (bed->elf_backend_output_arch_local_syms 11851 && (info->strip != strip_all || emit_relocs)) 11852 { 11853 typedef int (*out_sym_func) 11854 (void *, const char *, Elf_Internal_Sym *, asection *, 11855 struct elf_link_hash_entry *); 11856 11857 if (! ((*bed->elf_backend_output_arch_local_syms) 11858 (abfd, info, &flinfo, 11859 (out_sym_func) elf_link_output_symstrtab))) 11860 return FALSE; 11861 } 11862 11863 /* That wrote out all the local symbols. Finish up the symbol table 11864 with the global symbols. Even if we want to strip everything we 11865 can, we still need to deal with those global symbols that got 11866 converted to local in a version script. */ 11867 11868 /* The sh_info field records the index of the first non local symbol. */ 11869 symtab_hdr->sh_info = bfd_get_symcount (abfd); 11870 11871 if (dynamic 11872 && htab->dynsym != NULL 11873 && htab->dynsym->output_section != bfd_abs_section_ptr) 11874 { 11875 Elf_Internal_Sym sym; 11876 bfd_byte *dynsym = htab->dynsym->contents; 11877 11878 o = htab->dynsym->output_section; 11879 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; 11880 11881 /* Write out the section symbols for the output sections. */ 11882 if (bfd_link_pic (info) 11883 || htab->is_relocatable_executable) 11884 { 11885 asection *s; 11886 11887 sym.st_size = 0; 11888 sym.st_name = 0; 11889 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 11890 sym.st_other = 0; 11891 sym.st_target_internal = 0; 11892 11893 for (s = abfd->sections; s != NULL; s = s->next) 11894 { 11895 int indx; 11896 bfd_byte *dest; 11897 long dynindx; 11898 11899 dynindx = elf_section_data (s)->dynindx; 11900 if (dynindx <= 0) 11901 continue; 11902 indx = elf_section_data (s)->this_idx; 11903 BFD_ASSERT (indx > 0); 11904 sym.st_shndx = indx; 11905 if (! check_dynsym (abfd, &sym)) 11906 return FALSE; 11907 sym.st_value = s->vma; 11908 dest = dynsym + dynindx * bed->s->sizeof_sym; 11909 bed->s->swap_symbol_out (abfd, &sym, dest, 0); 11910 } 11911 } 11912 11913 /* Write out the local dynsyms. */ 11914 if (htab->dynlocal) 11915 { 11916 struct elf_link_local_dynamic_entry *e; 11917 for (e = htab->dynlocal; e ; e = e->next) 11918 { 11919 asection *s; 11920 bfd_byte *dest; 11921 11922 /* Copy the internal symbol and turn off visibility. 11923 Note that we saved a word of storage and overwrote 11924 the original st_name with the dynstr_index. */ 11925 sym = e->isym; 11926 sym.st_other &= ~ELF_ST_VISIBILITY (-1); 11927 11928 s = bfd_section_from_elf_index (e->input_bfd, 11929 e->isym.st_shndx); 11930 if (s != NULL) 11931 { 11932 sym.st_shndx = 11933 elf_section_data (s->output_section)->this_idx; 11934 if (! check_dynsym (abfd, &sym)) 11935 return FALSE; 11936 sym.st_value = (s->output_section->vma 11937 + s->output_offset 11938 + e->isym.st_value); 11939 } 11940 11941 dest = dynsym + e->dynindx * bed->s->sizeof_sym; 11942 bed->s->swap_symbol_out (abfd, &sym, dest, 0); 11943 } 11944 } 11945 } 11946 11947 /* We get the global symbols from the hash table. */ 11948 eoinfo.failed = FALSE; 11949 eoinfo.localsyms = FALSE; 11950 eoinfo.flinfo = &flinfo; 11951 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); 11952 if (eoinfo.failed) 11953 return FALSE; 11954 11955 /* If backend needs to output some symbols not present in the hash 11956 table, do it now. */ 11957 if (bed->elf_backend_output_arch_syms 11958 && (info->strip != strip_all || emit_relocs)) 11959 { 11960 typedef int (*out_sym_func) 11961 (void *, const char *, Elf_Internal_Sym *, asection *, 11962 struct elf_link_hash_entry *); 11963 11964 if (! ((*bed->elf_backend_output_arch_syms) 11965 (abfd, info, &flinfo, 11966 (out_sym_func) elf_link_output_symstrtab))) 11967 return FALSE; 11968 } 11969 11970 /* Finalize the .strtab section. */ 11971 _bfd_elf_strtab_finalize (flinfo.symstrtab); 11972 11973 /* Swap out the .strtab section. */ 11974 if (!elf_link_swap_symbols_out (&flinfo)) 11975 return FALSE; 11976 11977 /* Now we know the size of the symtab section. */ 11978 if (bfd_get_symcount (abfd) > 0) 11979 { 11980 /* Finish up and write out the symbol string table (.strtab) 11981 section. */ 11982 Elf_Internal_Shdr *symstrtab_hdr; 11983 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; 11984 11985 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; 11986 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) 11987 { 11988 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 11989 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 11990 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 11991 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); 11992 symtab_shndx_hdr->sh_size = amt; 11993 11994 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, 11995 off, TRUE); 11996 11997 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 11998 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) 11999 return FALSE; 12000 } 12001 12002 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 12003 /* sh_name was set in prep_headers. */ 12004 symstrtab_hdr->sh_type = SHT_STRTAB; 12005 symstrtab_hdr->sh_flags = bed->elf_strtab_flags; 12006 symstrtab_hdr->sh_addr = 0; 12007 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); 12008 symstrtab_hdr->sh_entsize = 0; 12009 symstrtab_hdr->sh_link = 0; 12010 symstrtab_hdr->sh_info = 0; 12011 /* sh_offset is set just below. */ 12012 symstrtab_hdr->sh_addralign = 1; 12013 12014 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, 12015 off, TRUE); 12016 elf_next_file_pos (abfd) = off; 12017 12018 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 12019 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) 12020 return FALSE; 12021 } 12022 12023 if (info->out_implib_bfd && !elf_output_implib (abfd, info)) 12024 { 12025 _bfd_error_handler (_("%B: failed to generate import library"), 12026 info->out_implib_bfd); 12027 return FALSE; 12028 } 12029 12030 /* Adjust the relocs to have the correct symbol indices. */ 12031 for (o = abfd->sections; o != NULL; o = o->next) 12032 { 12033 struct bfd_elf_section_data *esdo = elf_section_data (o); 12034 bfd_boolean sort; 12035 if ((o->flags & SEC_RELOC) == 0) 12036 continue; 12037 12038 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); 12039 if (esdo->rel.hdr != NULL 12040 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort)) 12041 return FALSE; 12042 if (esdo->rela.hdr != NULL 12043 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort)) 12044 return FALSE; 12045 12046 /* Set the reloc_count field to 0 to prevent write_relocs from 12047 trying to swap the relocs out itself. */ 12048 o->reloc_count = 0; 12049 } 12050 12051 if (dynamic && info->combreloc && dynobj != NULL) 12052 relativecount = elf_link_sort_relocs (abfd, info, &reldyn); 12053 12054 /* If we are linking against a dynamic object, or generating a 12055 shared library, finish up the dynamic linking information. */ 12056 if (dynamic) 12057 { 12058 bfd_byte *dyncon, *dynconend; 12059 12060 /* Fix up .dynamic entries. */ 12061 o = bfd_get_linker_section (dynobj, ".dynamic"); 12062 BFD_ASSERT (o != NULL); 12063 12064 dyncon = o->contents; 12065 dynconend = o->contents + o->size; 12066 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) 12067 { 12068 Elf_Internal_Dyn dyn; 12069 const char *name; 12070 unsigned int type; 12071 bfd_size_type sh_size; 12072 bfd_vma sh_addr; 12073 12074 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 12075 12076 switch (dyn.d_tag) 12077 { 12078 default: 12079 continue; 12080 case DT_NULL: 12081 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) 12082 { 12083 switch (elf_section_data (reldyn)->this_hdr.sh_type) 12084 { 12085 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; 12086 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; 12087 default: continue; 12088 } 12089 dyn.d_un.d_val = relativecount; 12090 relativecount = 0; 12091 break; 12092 } 12093 continue; 12094 12095 case DT_INIT: 12096 name = info->init_function; 12097 goto get_sym; 12098 case DT_FINI: 12099 name = info->fini_function; 12100 get_sym: 12101 { 12102 struct elf_link_hash_entry *h; 12103 12104 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); 12105 if (h != NULL 12106 && (h->root.type == bfd_link_hash_defined 12107 || h->root.type == bfd_link_hash_defweak)) 12108 { 12109 dyn.d_un.d_ptr = h->root.u.def.value; 12110 o = h->root.u.def.section; 12111 if (o->output_section != NULL) 12112 dyn.d_un.d_ptr += (o->output_section->vma 12113 + o->output_offset); 12114 else 12115 { 12116 /* The symbol is imported from another shared 12117 library and does not apply to this one. */ 12118 dyn.d_un.d_ptr = 0; 12119 } 12120 break; 12121 } 12122 } 12123 continue; 12124 12125 case DT_PREINIT_ARRAYSZ: 12126 name = ".preinit_array"; 12127 goto get_out_size; 12128 case DT_INIT_ARRAYSZ: 12129 name = ".init_array"; 12130 goto get_out_size; 12131 case DT_FINI_ARRAYSZ: 12132 name = ".fini_array"; 12133 get_out_size: 12134 o = bfd_get_section_by_name (abfd, name); 12135 if (o == NULL) 12136 { 12137 _bfd_error_handler 12138 (_("could not find section %s"), name); 12139 goto error_return; 12140 } 12141 if (o->size == 0) 12142 _bfd_error_handler 12143 (_("warning: %s section has zero size"), name); 12144 dyn.d_un.d_val = o->size; 12145 break; 12146 12147 case DT_PREINIT_ARRAY: 12148 name = ".preinit_array"; 12149 goto get_out_vma; 12150 case DT_INIT_ARRAY: 12151 name = ".init_array"; 12152 goto get_out_vma; 12153 case DT_FINI_ARRAY: 12154 name = ".fini_array"; 12155 get_out_vma: 12156 o = bfd_get_section_by_name (abfd, name); 12157 goto do_vma; 12158 12159 case DT_HASH: 12160 name = ".hash"; 12161 goto get_vma; 12162 case DT_GNU_HASH: 12163 name = ".gnu.hash"; 12164 goto get_vma; 12165 case DT_STRTAB: 12166 name = ".dynstr"; 12167 goto get_vma; 12168 case DT_SYMTAB: 12169 name = ".dynsym"; 12170 goto get_vma; 12171 case DT_VERDEF: 12172 name = ".gnu.version_d"; 12173 goto get_vma; 12174 case DT_VERNEED: 12175 name = ".gnu.version_r"; 12176 goto get_vma; 12177 case DT_VERSYM: 12178 name = ".gnu.version"; 12179 get_vma: 12180 o = bfd_get_linker_section (dynobj, name); 12181 do_vma: 12182 if (o == NULL) 12183 { 12184 _bfd_error_handler 12185 (_("could not find section %s"), name); 12186 goto error_return; 12187 } 12188 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) 12189 { 12190 _bfd_error_handler 12191 (_("warning: section '%s' is being made into a note"), name); 12192 bfd_set_error (bfd_error_nonrepresentable_section); 12193 goto error_return; 12194 } 12195 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; 12196 break; 12197 12198 case DT_REL: 12199 case DT_RELA: 12200 case DT_RELSZ: 12201 case DT_RELASZ: 12202 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) 12203 type = SHT_REL; 12204 else 12205 type = SHT_RELA; 12206 sh_size = 0; 12207 sh_addr = 0; 12208 for (i = 1; i < elf_numsections (abfd); i++) 12209 { 12210 Elf_Internal_Shdr *hdr; 12211 12212 hdr = elf_elfsections (abfd)[i]; 12213 if (hdr->sh_type == type 12214 && (hdr->sh_flags & SHF_ALLOC) != 0) 12215 { 12216 sh_size += hdr->sh_size; 12217 if (sh_addr == 0 12218 || sh_addr > hdr->sh_addr) 12219 sh_addr = hdr->sh_addr; 12220 } 12221 } 12222 12223 if (bed->dtrel_excludes_plt && htab->srelplt != NULL) 12224 { 12225 /* Don't count procedure linkage table relocs in the 12226 overall reloc count. */ 12227 sh_size -= htab->srelplt->size; 12228 if (sh_size == 0) 12229 /* If the size is zero, make the address zero too. 12230 This is to avoid a glibc bug. If the backend 12231 emits DT_RELA/DT_RELASZ even when DT_RELASZ is 12232 zero, then we'll put DT_RELA at the end of 12233 DT_JMPREL. glibc will interpret the end of 12234 DT_RELA matching the end of DT_JMPREL as the 12235 case where DT_RELA includes DT_JMPREL, and for 12236 LD_BIND_NOW will decide that processing DT_RELA 12237 will process the PLT relocs too. Net result: 12238 No PLT relocs applied. */ 12239 sh_addr = 0; 12240 12241 /* If .rela.plt is the first .rela section, exclude 12242 it from DT_RELA. */ 12243 else if (sh_addr == (htab->srelplt->output_section->vma 12244 + htab->srelplt->output_offset)) 12245 sh_addr += htab->srelplt->size; 12246 } 12247 12248 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) 12249 dyn.d_un.d_val = sh_size; 12250 else 12251 dyn.d_un.d_ptr = sh_addr; 12252 break; 12253 } 12254 bed->s->swap_dyn_out (dynobj, &dyn, dyncon); 12255 } 12256 } 12257 12258 /* If we have created any dynamic sections, then output them. */ 12259 if (dynobj != NULL) 12260 { 12261 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) 12262 goto error_return; 12263 12264 /* Check for DT_TEXTREL (late, in case the backend removes it). */ 12265 if (((info->warn_shared_textrel && bfd_link_pic (info)) 12266 || info->error_textrel) 12267 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) 12268 { 12269 bfd_byte *dyncon, *dynconend; 12270 12271 dyncon = o->contents; 12272 dynconend = o->contents + o->size; 12273 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) 12274 { 12275 Elf_Internal_Dyn dyn; 12276 12277 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 12278 12279 if (dyn.d_tag == DT_TEXTREL) 12280 { 12281 if (info->error_textrel) 12282 info->callbacks->einfo 12283 (_("%P%X: read-only segment has dynamic relocations.\n")); 12284 else 12285 info->callbacks->einfo 12286 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); 12287 break; 12288 } 12289 } 12290 } 12291 12292 for (o = dynobj->sections; o != NULL; o = o->next) 12293 { 12294 if ((o->flags & SEC_HAS_CONTENTS) == 0 12295 || o->size == 0 12296 || o->output_section == bfd_abs_section_ptr) 12297 continue; 12298 if ((o->flags & SEC_LINKER_CREATED) == 0) 12299 { 12300 /* At this point, we are only interested in sections 12301 created by _bfd_elf_link_create_dynamic_sections. */ 12302 continue; 12303 } 12304 if (htab->stab_info.stabstr == o) 12305 continue; 12306 if (htab->eh_info.hdr_sec == o) 12307 continue; 12308 if (strcmp (o->name, ".dynstr") != 0) 12309 { 12310 if (! bfd_set_section_contents (abfd, o->output_section, 12311 o->contents, 12312 (file_ptr) o->output_offset 12313 * bfd_octets_per_byte (abfd), 12314 o->size)) 12315 goto error_return; 12316 } 12317 else 12318 { 12319 /* The contents of the .dynstr section are actually in a 12320 stringtab. */ 12321 file_ptr off; 12322 12323 off = elf_section_data (o->output_section)->this_hdr.sh_offset; 12324 if (bfd_seek (abfd, off, SEEK_SET) != 0 12325 || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) 12326 goto error_return; 12327 } 12328 } 12329 } 12330 12331 if (bfd_link_relocatable (info)) 12332 { 12333 bfd_boolean failed = FALSE; 12334 12335 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 12336 if (failed) 12337 goto error_return; 12338 } 12339 12340 /* If we have optimized stabs strings, output them. */ 12341 if (htab->stab_info.stabstr != NULL) 12342 { 12343 if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) 12344 goto error_return; 12345 } 12346 12347 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) 12348 goto error_return; 12349 12350 elf_final_link_free (abfd, &flinfo); 12351 12352 elf_linker (abfd) = TRUE; 12353 12354 if (attr_section) 12355 { 12356 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); 12357 if (contents == NULL) 12358 return FALSE; /* Bail out and fail. */ 12359 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); 12360 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); 12361 free (contents); 12362 } 12363 12364 return TRUE; 12365 12366 error_return: 12367 elf_final_link_free (abfd, &flinfo); 12368 return FALSE; 12369} 12370 12371/* Initialize COOKIE for input bfd ABFD. */ 12372 12373static bfd_boolean 12374init_reloc_cookie (struct elf_reloc_cookie *cookie, 12375 struct bfd_link_info *info, bfd *abfd) 12376{ 12377 Elf_Internal_Shdr *symtab_hdr; 12378 const struct elf_backend_data *bed; 12379 12380 bed = get_elf_backend_data (abfd); 12381 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 12382 12383 cookie->abfd = abfd; 12384 cookie->sym_hashes = elf_sym_hashes (abfd); 12385 cookie->bad_symtab = elf_bad_symtab (abfd); 12386 if (cookie->bad_symtab) 12387 { 12388 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 12389 cookie->extsymoff = 0; 12390 } 12391 else 12392 { 12393 cookie->locsymcount = symtab_hdr->sh_info; 12394 cookie->extsymoff = symtab_hdr->sh_info; 12395 } 12396 12397 if (bed->s->arch_size == 32) 12398 cookie->r_sym_shift = 8; 12399 else 12400 cookie->r_sym_shift = 32; 12401 12402 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; 12403 if (cookie->locsyms == NULL && cookie->locsymcount != 0) 12404 { 12405 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, 12406 cookie->locsymcount, 0, 12407 NULL, NULL, NULL); 12408 if (cookie->locsyms == NULL) 12409 { 12410 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); 12411 return FALSE; 12412 } 12413 if (info->keep_memory) 12414 symtab_hdr->contents = (bfd_byte *) cookie->locsyms; 12415 } 12416 return TRUE; 12417} 12418 12419/* Free the memory allocated by init_reloc_cookie, if appropriate. */ 12420 12421static void 12422fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) 12423{ 12424 Elf_Internal_Shdr *symtab_hdr; 12425 12426 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 12427 if (cookie->locsyms != NULL 12428 && symtab_hdr->contents != (unsigned char *) cookie->locsyms) 12429 free (cookie->locsyms); 12430} 12431 12432/* Initialize the relocation information in COOKIE for input section SEC 12433 of input bfd ABFD. */ 12434 12435static bfd_boolean 12436init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, 12437 struct bfd_link_info *info, bfd *abfd, 12438 asection *sec) 12439{ 12440 const struct elf_backend_data *bed; 12441 12442 if (sec->reloc_count == 0) 12443 { 12444 cookie->rels = NULL; 12445 cookie->relend = NULL; 12446 } 12447 else 12448 { 12449 bed = get_elf_backend_data (abfd); 12450 12451 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, 12452 info->keep_memory); 12453 if (cookie->rels == NULL) 12454 return FALSE; 12455 cookie->rel = cookie->rels; 12456 cookie->relend = (cookie->rels 12457 + sec->reloc_count * bed->s->int_rels_per_ext_rel); 12458 } 12459 cookie->rel = cookie->rels; 12460 return TRUE; 12461} 12462 12463/* Free the memory allocated by init_reloc_cookie_rels, 12464 if appropriate. */ 12465 12466static void 12467fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, 12468 asection *sec) 12469{ 12470 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) 12471 free (cookie->rels); 12472} 12473 12474/* Initialize the whole of COOKIE for input section SEC. */ 12475 12476static bfd_boolean 12477init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, 12478 struct bfd_link_info *info, 12479 asection *sec) 12480{ 12481 if (!init_reloc_cookie (cookie, info, sec->owner)) 12482 goto error1; 12483 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) 12484 goto error2; 12485 return TRUE; 12486 12487 error2: 12488 fini_reloc_cookie (cookie, sec->owner); 12489 error1: 12490 return FALSE; 12491} 12492 12493/* Free the memory allocated by init_reloc_cookie_for_section, 12494 if appropriate. */ 12495 12496static void 12497fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, 12498 asection *sec) 12499{ 12500 fini_reloc_cookie_rels (cookie, sec); 12501 fini_reloc_cookie (cookie, sec->owner); 12502} 12503 12504/* Garbage collect unused sections. */ 12505 12506/* Default gc_mark_hook. */ 12507 12508asection * 12509_bfd_elf_gc_mark_hook (asection *sec, 12510 struct bfd_link_info *info ATTRIBUTE_UNUSED, 12511 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, 12512 struct elf_link_hash_entry *h, 12513 Elf_Internal_Sym *sym) 12514{ 12515 if (h != NULL) 12516 { 12517 switch (h->root.type) 12518 { 12519 case bfd_link_hash_defined: 12520 case bfd_link_hash_defweak: 12521 return h->root.u.def.section; 12522 12523 case bfd_link_hash_common: 12524 return h->root.u.c.p->section; 12525 12526 default: 12527 break; 12528 } 12529 } 12530 else 12531 return bfd_section_from_elf_index (sec->owner, sym->st_shndx); 12532 12533 return NULL; 12534} 12535 12536/* For undefined __start_<name> and __stop_<name> symbols, return the 12537 first input section matching <name>. Return NULL otherwise. */ 12538 12539asection * 12540_bfd_elf_is_start_stop (const struct bfd_link_info *info, 12541 struct elf_link_hash_entry *h) 12542{ 12543 asection *s; 12544 const char *sec_name; 12545 12546 if (h->root.type != bfd_link_hash_undefined 12547 && h->root.type != bfd_link_hash_undefweak) 12548 return NULL; 12549 12550 s = h->root.u.undef.section; 12551 if (s != NULL) 12552 { 12553 if (s == (asection *) 0 - 1) 12554 return NULL; 12555 return s; 12556 } 12557 12558 sec_name = NULL; 12559 if (strncmp (h->root.root.string, "__start_", 8) == 0) 12560 sec_name = h->root.root.string + 8; 12561 else if (strncmp (h->root.root.string, "__stop_", 7) == 0) 12562 sec_name = h->root.root.string + 7; 12563 12564 if (sec_name != NULL && *sec_name != '\0') 12565 { 12566 bfd *i; 12567 12568 for (i = info->input_bfds; i != NULL; i = i->link.next) 12569 { 12570 s = bfd_get_section_by_name (i, sec_name); 12571 if (s != NULL) 12572 { 12573 h->root.u.undef.section = s; 12574 break; 12575 } 12576 } 12577 } 12578 12579 if (s == NULL) 12580 h->root.u.undef.section = (asection *) 0 - 1; 12581 12582 return s; 12583} 12584 12585/* COOKIE->rel describes a relocation against section SEC, which is 12586 a section we've decided to keep. Return the section that contains 12587 the relocation symbol, or NULL if no section contains it. */ 12588 12589asection * 12590_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, 12591 elf_gc_mark_hook_fn gc_mark_hook, 12592 struct elf_reloc_cookie *cookie, 12593 bfd_boolean *start_stop) 12594{ 12595 unsigned long r_symndx; 12596 struct elf_link_hash_entry *h; 12597 12598 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; 12599 if (r_symndx == STN_UNDEF) 12600 return NULL; 12601 12602 if (r_symndx >= cookie->locsymcount 12603 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) 12604 { 12605 h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; 12606 if (h == NULL) 12607 { 12608 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"), 12609 sec->owner); 12610 return NULL; 12611 } 12612 while (h->root.type == bfd_link_hash_indirect 12613 || h->root.type == bfd_link_hash_warning) 12614 h = (struct elf_link_hash_entry *) h->root.u.i.link; 12615 h->mark = 1; 12616 /* If this symbol is weak and there is a non-weak definition, we 12617 keep the non-weak definition because many backends put 12618 dynamic reloc info on the non-weak definition for code 12619 handling copy relocs. */ 12620 if (h->u.weakdef != NULL) 12621 h->u.weakdef->mark = 1; 12622 12623 if (start_stop != NULL) 12624 { 12625 /* To work around a glibc bug, mark all XXX input sections 12626 when there is an as yet undefined reference to __start_XXX 12627 or __stop_XXX symbols. The linker will later define such 12628 symbols for orphan input sections that have a name 12629 representable as a C identifier. */ 12630 asection *s = _bfd_elf_is_start_stop (info, h); 12631 12632 if (s != NULL) 12633 { 12634 *start_stop = !s->gc_mark; 12635 return s; 12636 } 12637 } 12638 12639 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); 12640 } 12641 12642 return (*gc_mark_hook) (sec, info, cookie->rel, NULL, 12643 &cookie->locsyms[r_symndx]); 12644} 12645 12646/* COOKIE->rel describes a relocation against section SEC, which is 12647 a section we've decided to keep. Mark the section that contains 12648 the relocation symbol. */ 12649 12650bfd_boolean 12651_bfd_elf_gc_mark_reloc (struct bfd_link_info *info, 12652 asection *sec, 12653 elf_gc_mark_hook_fn gc_mark_hook, 12654 struct elf_reloc_cookie *cookie) 12655{ 12656 asection *rsec; 12657 bfd_boolean start_stop = FALSE; 12658 12659 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); 12660 while (rsec != NULL) 12661 { 12662 if (!rsec->gc_mark) 12663 { 12664 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour 12665 || (rsec->owner->flags & DYNAMIC) != 0) 12666 rsec->gc_mark = 1; 12667 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) 12668 return FALSE; 12669 } 12670 if (!start_stop) 12671 break; 12672 rsec = bfd_get_next_section_by_name (rsec->owner, rsec); 12673 } 12674 return TRUE; 12675} 12676 12677/* The mark phase of garbage collection. For a given section, mark 12678 it and any sections in this section's group, and all the sections 12679 which define symbols to which it refers. */ 12680 12681bfd_boolean 12682_bfd_elf_gc_mark (struct bfd_link_info *info, 12683 asection *sec, 12684 elf_gc_mark_hook_fn gc_mark_hook) 12685{ 12686 bfd_boolean ret; 12687 asection *group_sec, *eh_frame; 12688 12689 sec->gc_mark = 1; 12690 12691 /* Mark all the sections in the group. */ 12692 group_sec = elf_section_data (sec)->next_in_group; 12693 if (group_sec && !group_sec->gc_mark) 12694 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) 12695 return FALSE; 12696 12697 /* Look through the section relocs. */ 12698 ret = TRUE; 12699 eh_frame = elf_eh_frame_section (sec->owner); 12700 if ((sec->flags & SEC_RELOC) != 0 12701 && sec->reloc_count > 0 12702 && sec != eh_frame) 12703 { 12704 struct elf_reloc_cookie cookie; 12705 12706 if (!init_reloc_cookie_for_section (&cookie, info, sec)) 12707 ret = FALSE; 12708 else 12709 { 12710 for (; cookie.rel < cookie.relend; cookie.rel++) 12711 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) 12712 { 12713 ret = FALSE; 12714 break; 12715 } 12716 fini_reloc_cookie_for_section (&cookie, sec); 12717 } 12718 } 12719 12720 if (ret && eh_frame && elf_fde_list (sec)) 12721 { 12722 struct elf_reloc_cookie cookie; 12723 12724 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) 12725 ret = FALSE; 12726 else 12727 { 12728 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, 12729 gc_mark_hook, &cookie)) 12730 ret = FALSE; 12731 fini_reloc_cookie_for_section (&cookie, eh_frame); 12732 } 12733 } 12734 12735 eh_frame = elf_section_eh_frame_entry (sec); 12736 if (ret && eh_frame && !eh_frame->gc_mark) 12737 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) 12738 ret = FALSE; 12739 12740 return ret; 12741} 12742 12743/* Scan and mark sections in a special or debug section group. */ 12744 12745static void 12746_bfd_elf_gc_mark_debug_special_section_group (asection *grp) 12747{ 12748 /* Point to first section of section group. */ 12749 asection *ssec; 12750 /* Used to iterate the section group. */ 12751 asection *msec; 12752 12753 bfd_boolean is_special_grp = TRUE; 12754 bfd_boolean is_debug_grp = TRUE; 12755 12756 /* First scan to see if group contains any section other than debug 12757 and special section. */ 12758 ssec = msec = elf_next_in_group (grp); 12759 do 12760 { 12761 if ((msec->flags & SEC_DEBUGGING) == 0) 12762 is_debug_grp = FALSE; 12763 12764 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) 12765 is_special_grp = FALSE; 12766 12767 msec = elf_next_in_group (msec); 12768 } 12769 while (msec != ssec); 12770 12771 /* If this is a pure debug section group or pure special section group, 12772 keep all sections in this group. */ 12773 if (is_debug_grp || is_special_grp) 12774 { 12775 do 12776 { 12777 msec->gc_mark = 1; 12778 msec = elf_next_in_group (msec); 12779 } 12780 while (msec != ssec); 12781 } 12782} 12783 12784/* Keep debug and special sections. */ 12785 12786bfd_boolean 12787_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, 12788 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) 12789{ 12790 bfd *ibfd; 12791 12792 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 12793 { 12794 asection *isec; 12795 bfd_boolean some_kept; 12796 bfd_boolean debug_frag_seen; 12797 12798 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 12799 continue; 12800 12801 /* Ensure all linker created sections are kept, 12802 see if any other section is already marked, 12803 and note if we have any fragmented debug sections. */ 12804 debug_frag_seen = some_kept = FALSE; 12805 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 12806 { 12807 if ((isec->flags & SEC_LINKER_CREATED) != 0) 12808 isec->gc_mark = 1; 12809 else if (isec->gc_mark) 12810 some_kept = TRUE; 12811 12812 if (debug_frag_seen == FALSE 12813 && (isec->flags & SEC_DEBUGGING) 12814 && CONST_STRNEQ (isec->name, ".debug_line.")) 12815 debug_frag_seen = TRUE; 12816 } 12817 12818 /* If no section in this file will be kept, then we can 12819 toss out the debug and special sections. */ 12820 if (!some_kept) 12821 continue; 12822 12823 /* Keep debug and special sections like .comment when they are 12824 not part of a group. Also keep section groups that contain 12825 just debug sections or special sections. */ 12826 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 12827 { 12828 if ((isec->flags & SEC_GROUP) != 0) 12829 _bfd_elf_gc_mark_debug_special_section_group (isec); 12830 else if (((isec->flags & SEC_DEBUGGING) != 0 12831 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) 12832 && elf_next_in_group (isec) == NULL) 12833 isec->gc_mark = 1; 12834 } 12835 12836 if (! debug_frag_seen) 12837 continue; 12838 12839 /* Look for CODE sections which are going to be discarded, 12840 and find and discard any fragmented debug sections which 12841 are associated with that code section. */ 12842 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 12843 if ((isec->flags & SEC_CODE) != 0 12844 && isec->gc_mark == 0) 12845 { 12846 unsigned int ilen; 12847 asection *dsec; 12848 12849 ilen = strlen (isec->name); 12850 12851 /* Association is determined by the name of the debug section 12852 containing the name of the code section as a suffix. For 12853 example .debug_line.text.foo is a debug section associated 12854 with .text.foo. */ 12855 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) 12856 { 12857 unsigned int dlen; 12858 12859 if (dsec->gc_mark == 0 12860 || (dsec->flags & SEC_DEBUGGING) == 0) 12861 continue; 12862 12863 dlen = strlen (dsec->name); 12864 12865 if (dlen > ilen 12866 && strncmp (dsec->name + (dlen - ilen), 12867 isec->name, ilen) == 0) 12868 { 12869 dsec->gc_mark = 0; 12870 } 12871 } 12872 } 12873 } 12874 return TRUE; 12875} 12876 12877/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ 12878 12879struct elf_gc_sweep_symbol_info 12880{ 12881 struct bfd_link_info *info; 12882 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, 12883 bfd_boolean); 12884}; 12885 12886static bfd_boolean 12887elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) 12888{ 12889 if (!h->mark 12890 && (((h->root.type == bfd_link_hash_defined 12891 || h->root.type == bfd_link_hash_defweak) 12892 && !((h->def_regular || ELF_COMMON_DEF_P (h)) 12893 && h->root.u.def.section->gc_mark)) 12894 || h->root.type == bfd_link_hash_undefined 12895 || h->root.type == bfd_link_hash_undefweak)) 12896 { 12897 struct elf_gc_sweep_symbol_info *inf; 12898 12899 inf = (struct elf_gc_sweep_symbol_info *) data; 12900 (*inf->hide_symbol) (inf->info, h, TRUE); 12901 h->def_regular = 0; 12902 h->ref_regular = 0; 12903 h->ref_regular_nonweak = 0; 12904 } 12905 12906 return TRUE; 12907} 12908 12909/* The sweep phase of garbage collection. Remove all garbage sections. */ 12910 12911typedef bfd_boolean (*gc_sweep_hook_fn) 12912 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); 12913 12914static bfd_boolean 12915elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) 12916{ 12917 bfd *sub; 12918 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12919 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; 12920 unsigned long section_sym_count; 12921 struct elf_gc_sweep_symbol_info sweep_info; 12922 12923 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 12924 { 12925 asection *o; 12926 12927 if (bfd_get_flavour (sub) != bfd_target_elf_flavour 12928 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) 12929 continue; 12930 12931 for (o = sub->sections; o != NULL; o = o->next) 12932 { 12933 /* When any section in a section group is kept, we keep all 12934 sections in the section group. If the first member of 12935 the section group is excluded, we will also exclude the 12936 group section. */ 12937 if (o->flags & SEC_GROUP) 12938 { 12939 asection *first = elf_next_in_group (o); 12940 o->gc_mark = first->gc_mark; 12941 } 12942 12943 if (o->gc_mark) 12944 continue; 12945 12946 /* Skip sweeping sections already excluded. */ 12947 if (o->flags & SEC_EXCLUDE) 12948 continue; 12949 12950 /* Since this is early in the link process, it is simple 12951 to remove a section from the output. */ 12952 o->flags |= SEC_EXCLUDE; 12953 12954 if (info->print_gc_sections && o->size != 0) 12955 /* xgettext:c-format */ 12956 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); 12957 12958 /* But we also have to update some of the relocation 12959 info we collected before. */ 12960 if (gc_sweep_hook 12961 && (o->flags & SEC_RELOC) != 0 12962 && o->reloc_count != 0 12963 && !((info->strip == strip_all || info->strip == strip_debugger) 12964 && (o->flags & SEC_DEBUGGING) != 0) 12965 && !bfd_is_abs_section (o->output_section)) 12966 { 12967 Elf_Internal_Rela *internal_relocs; 12968 bfd_boolean r; 12969 12970 internal_relocs 12971 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, 12972 info->keep_memory); 12973 if (internal_relocs == NULL) 12974 return FALSE; 12975 12976 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); 12977 12978 if (elf_section_data (o)->relocs != internal_relocs) 12979 free (internal_relocs); 12980 12981 if (!r) 12982 return FALSE; 12983 } 12984 } 12985 } 12986 12987 /* Remove the symbols that were in the swept sections from the dynamic 12988 symbol table. GCFIXME: Anyone know how to get them out of the 12989 static symbol table as well? */ 12990 sweep_info.info = info; 12991 sweep_info.hide_symbol = bed->elf_backend_hide_symbol; 12992 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, 12993 &sweep_info); 12994 12995 /* We need to reassign dynsym indices now that symbols may have 12996 been removed. See the call in `bfd_elf_size_dynsym_hash_dynstr' 12997 for the details of the conditions used here. */ 12998 if (elf_hash_table (info)->dynamic_sections_created 12999 || bed->always_renumber_dynsyms) 13000 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); 13001 return TRUE; 13002} 13003 13004/* Propagate collected vtable information. This is called through 13005 elf_link_hash_traverse. */ 13006 13007static bfd_boolean 13008elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) 13009{ 13010 /* Those that are not vtables. */ 13011 if (h->vtable == NULL || h->vtable->parent == NULL) 13012 return TRUE; 13013 13014 /* Those vtables that do not have parents, we cannot merge. */ 13015 if (h->vtable->parent == (struct elf_link_hash_entry *) -1) 13016 return TRUE; 13017 13018 /* If we've already been done, exit. */ 13019 if (h->vtable->used && h->vtable->used[-1]) 13020 return TRUE; 13021 13022 /* Make sure the parent's table is up to date. */ 13023 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); 13024 13025 if (h->vtable->used == NULL) 13026 { 13027 /* None of this table's entries were referenced. Re-use the 13028 parent's table. */ 13029 h->vtable->used = h->vtable->parent->vtable->used; 13030 h->vtable->size = h->vtable->parent->vtable->size; 13031 } 13032 else 13033 { 13034 size_t n; 13035 bfd_boolean *cu, *pu; 13036 13037 /* Or the parent's entries into ours. */ 13038 cu = h->vtable->used; 13039 cu[-1] = TRUE; 13040 pu = h->vtable->parent->vtable->used; 13041 if (pu != NULL) 13042 { 13043 const struct elf_backend_data *bed; 13044 unsigned int log_file_align; 13045 13046 bed = get_elf_backend_data (h->root.u.def.section->owner); 13047 log_file_align = bed->s->log_file_align; 13048 n = h->vtable->parent->vtable->size >> log_file_align; 13049 while (n--) 13050 { 13051 if (*pu) 13052 *cu = TRUE; 13053 pu++; 13054 cu++; 13055 } 13056 } 13057 } 13058 13059 return TRUE; 13060} 13061 13062static bfd_boolean 13063elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) 13064{ 13065 asection *sec; 13066 bfd_vma hstart, hend; 13067 Elf_Internal_Rela *relstart, *relend, *rel; 13068 const struct elf_backend_data *bed; 13069 unsigned int log_file_align; 13070 13071 /* Take care of both those symbols that do not describe vtables as 13072 well as those that are not loaded. */ 13073 if (h->vtable == NULL || h->vtable->parent == NULL) 13074 return TRUE; 13075 13076 BFD_ASSERT (h->root.type == bfd_link_hash_defined 13077 || h->root.type == bfd_link_hash_defweak); 13078 13079 sec = h->root.u.def.section; 13080 hstart = h->root.u.def.value; 13081 hend = hstart + h->size; 13082 13083 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); 13084 if (!relstart) 13085 return *(bfd_boolean *) okp = FALSE; 13086 bed = get_elf_backend_data (sec->owner); 13087 log_file_align = bed->s->log_file_align; 13088 13089 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; 13090 13091 for (rel = relstart; rel < relend; ++rel) 13092 if (rel->r_offset >= hstart && rel->r_offset < hend) 13093 { 13094 /* If the entry is in use, do nothing. */ 13095 if (h->vtable->used 13096 && (rel->r_offset - hstart) < h->vtable->size) 13097 { 13098 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; 13099 if (h->vtable->used[entry]) 13100 continue; 13101 } 13102 /* Otherwise, kill it. */ 13103 rel->r_offset = rel->r_info = rel->r_addend = 0; 13104 } 13105 13106 return TRUE; 13107} 13108 13109/* Mark sections containing dynamically referenced symbols. When 13110 building shared libraries, we must assume that any visible symbol is 13111 referenced. */ 13112 13113bfd_boolean 13114bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) 13115{ 13116 struct bfd_link_info *info = (struct bfd_link_info *) inf; 13117 struct bfd_elf_dynamic_list *d = info->dynamic_list; 13118 13119 if ((h->root.type == bfd_link_hash_defined 13120 || h->root.type == bfd_link_hash_defweak) 13121 && (h->ref_dynamic 13122 || ((h->def_regular || ELF_COMMON_DEF_P (h)) 13123 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL 13124 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN 13125 && (!bfd_link_executable (info) 13126 || info->gc_keep_exported 13127 || info->export_dynamic 13128 || (h->dynamic 13129 && d != NULL 13130 && (*d->match) (&d->head, NULL, h->root.root.string))) 13131 && (h->versioned >= versioned 13132 || !bfd_hide_sym_by_version (info->version_info, 13133 h->root.root.string))))) 13134 h->root.u.def.section->flags |= SEC_KEEP; 13135 13136 return TRUE; 13137} 13138 13139/* Keep all sections containing symbols undefined on the command-line, 13140 and the section containing the entry symbol. */ 13141 13142void 13143_bfd_elf_gc_keep (struct bfd_link_info *info) 13144{ 13145 struct bfd_sym_chain *sym; 13146 13147 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) 13148 { 13149 struct elf_link_hash_entry *h; 13150 13151 h = elf_link_hash_lookup (elf_hash_table (info), sym->name, 13152 FALSE, FALSE, FALSE); 13153 13154 if (h != NULL 13155 && (h->root.type == bfd_link_hash_defined 13156 || h->root.type == bfd_link_hash_defweak) 13157 && !bfd_is_abs_section (h->root.u.def.section) 13158 && !bfd_is_und_section (h->root.u.def.section)) 13159 h->root.u.def.section->flags |= SEC_KEEP; 13160 } 13161} 13162 13163bfd_boolean 13164bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, 13165 struct bfd_link_info *info) 13166{ 13167 bfd *ibfd = info->input_bfds; 13168 13169 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 13170 { 13171 asection *sec; 13172 struct elf_reloc_cookie cookie; 13173 13174 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 13175 continue; 13176 13177 if (!init_reloc_cookie (&cookie, info, ibfd)) 13178 return FALSE; 13179 13180 for (sec = ibfd->sections; sec; sec = sec->next) 13181 { 13182 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") 13183 && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) 13184 { 13185 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); 13186 fini_reloc_cookie_rels (&cookie, sec); 13187 } 13188 } 13189 } 13190 return TRUE; 13191} 13192 13193/* Do mark and sweep of unused sections. */ 13194 13195bfd_boolean 13196bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) 13197{ 13198 bfd_boolean ok = TRUE; 13199 bfd *sub; 13200 elf_gc_mark_hook_fn gc_mark_hook; 13201 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13202 struct elf_link_hash_table *htab; 13203 13204 if (!bed->can_gc_sections 13205 || !is_elf_hash_table (info->hash)) 13206 { 13207 _bfd_error_handler(_("Warning: gc-sections option ignored")); 13208 return TRUE; 13209 } 13210 13211 bed->gc_keep (info); 13212 htab = elf_hash_table (info); 13213 13214 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section 13215 at the .eh_frame section if we can mark the FDEs individually. */ 13216 for (sub = info->input_bfds; 13217 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; 13218 sub = sub->link.next) 13219 { 13220 asection *sec; 13221 struct elf_reloc_cookie cookie; 13222 13223 sec = bfd_get_section_by_name (sub, ".eh_frame"); 13224 while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) 13225 { 13226 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); 13227 if (elf_section_data (sec)->sec_info 13228 && (sec->flags & SEC_LINKER_CREATED) == 0) 13229 elf_eh_frame_section (sub) = sec; 13230 fini_reloc_cookie_for_section (&cookie, sec); 13231 sec = bfd_get_next_section_by_name (NULL, sec); 13232 } 13233 } 13234 13235 /* Apply transitive closure to the vtable entry usage info. */ 13236 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); 13237 if (!ok) 13238 return FALSE; 13239 13240 /* Kill the vtable relocations that were not used. */ 13241 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); 13242 if (!ok) 13243 return FALSE; 13244 13245 /* Mark dynamically referenced symbols. */ 13246 if (htab->dynamic_sections_created || info->gc_keep_exported) 13247 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); 13248 13249 /* Grovel through relocs to find out who stays ... */ 13250 gc_mark_hook = bed->gc_mark_hook; 13251 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 13252 { 13253 asection *o; 13254 13255 if (bfd_get_flavour (sub) != bfd_target_elf_flavour 13256 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) 13257 continue; 13258 13259 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). 13260 Also treat note sections as a root, if the section is not part 13261 of a group. */ 13262 for (o = sub->sections; o != NULL; o = o->next) 13263 if (!o->gc_mark 13264 && (o->flags & SEC_EXCLUDE) == 0 13265 && ((o->flags & SEC_KEEP) != 0 13266 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE 13267 && elf_next_in_group (o) == NULL ))) 13268 { 13269 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) 13270 return FALSE; 13271 } 13272 } 13273 13274 /* Allow the backend to mark additional target specific sections. */ 13275 bed->gc_mark_extra_sections (info, gc_mark_hook); 13276 13277 /* ... and mark SEC_EXCLUDE for those that go. */ 13278 return elf_gc_sweep (abfd, info); 13279} 13280 13281/* Called from check_relocs to record the existence of a VTINHERIT reloc. */ 13282 13283bfd_boolean 13284bfd_elf_gc_record_vtinherit (bfd *abfd, 13285 asection *sec, 13286 struct elf_link_hash_entry *h, 13287 bfd_vma offset) 13288{ 13289 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; 13290 struct elf_link_hash_entry **search, *child; 13291 size_t extsymcount; 13292 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13293 13294 /* The sh_info field of the symtab header tells us where the 13295 external symbols start. We don't care about the local symbols at 13296 this point. */ 13297 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; 13298 if (!elf_bad_symtab (abfd)) 13299 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; 13300 13301 sym_hashes = elf_sym_hashes (abfd); 13302 sym_hashes_end = sym_hashes + extsymcount; 13303 13304 /* Hunt down the child symbol, which is in this section at the same 13305 offset as the relocation. */ 13306 for (search = sym_hashes; search != sym_hashes_end; ++search) 13307 { 13308 if ((child = *search) != NULL 13309 && (child->root.type == bfd_link_hash_defined 13310 || child->root.type == bfd_link_hash_defweak) 13311 && child->root.u.def.section == sec 13312 && child->root.u.def.value == offset) 13313 goto win; 13314 } 13315 13316 /* xgettext:c-format */ 13317 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"), 13318 abfd, sec, (unsigned long) offset); 13319 bfd_set_error (bfd_error_invalid_operation); 13320 return FALSE; 13321 13322 win: 13323 if (!child->vtable) 13324 { 13325 child->vtable = ((struct elf_link_virtual_table_entry *) 13326 bfd_zalloc (abfd, sizeof (*child->vtable))); 13327 if (!child->vtable) 13328 return FALSE; 13329 } 13330 if (!h) 13331 { 13332 /* This *should* only be the absolute section. It could potentially 13333 be that someone has defined a non-global vtable though, which 13334 would be bad. It isn't worth paging in the local symbols to be 13335 sure though; that case should simply be handled by the assembler. */ 13336 13337 child->vtable->parent = (struct elf_link_hash_entry *) -1; 13338 } 13339 else 13340 child->vtable->parent = h; 13341 13342 return TRUE; 13343} 13344 13345/* Called from check_relocs to record the existence of a VTENTRY reloc. */ 13346 13347bfd_boolean 13348bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, 13349 asection *sec ATTRIBUTE_UNUSED, 13350 struct elf_link_hash_entry *h, 13351 bfd_vma addend) 13352{ 13353 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13354 unsigned int log_file_align = bed->s->log_file_align; 13355 13356 if (!h->vtable) 13357 { 13358 h->vtable = ((struct elf_link_virtual_table_entry *) 13359 bfd_zalloc (abfd, sizeof (*h->vtable))); 13360 if (!h->vtable) 13361 return FALSE; 13362 } 13363 13364 if (addend >= h->vtable->size) 13365 { 13366 size_t size, bytes, file_align; 13367 bfd_boolean *ptr = h->vtable->used; 13368 13369 /* While the symbol is undefined, we have to be prepared to handle 13370 a zero size. */ 13371 file_align = 1 << log_file_align; 13372 if (h->root.type == bfd_link_hash_undefined) 13373 size = addend + file_align; 13374 else 13375 { 13376 size = h->size; 13377 if (addend >= size) 13378 { 13379 /* Oops! We've got a reference past the defined end of 13380 the table. This is probably a bug -- shall we warn? */ 13381 size = addend + file_align; 13382 } 13383 } 13384 size = (size + file_align - 1) & -file_align; 13385 13386 /* Allocate one extra entry for use as a "done" flag for the 13387 consolidation pass. */ 13388 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); 13389 13390 if (ptr) 13391 { 13392 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); 13393 13394 if (ptr != NULL) 13395 { 13396 size_t oldbytes; 13397 13398 oldbytes = (((h->vtable->size >> log_file_align) + 1) 13399 * sizeof (bfd_boolean)); 13400 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); 13401 } 13402 } 13403 else 13404 ptr = (bfd_boolean *) bfd_zmalloc (bytes); 13405 13406 if (ptr == NULL) 13407 return FALSE; 13408 13409 /* And arrange for that done flag to be at index -1. */ 13410 h->vtable->used = ptr + 1; 13411 h->vtable->size = size; 13412 } 13413 13414 h->vtable->used[addend >> log_file_align] = TRUE; 13415 13416 return TRUE; 13417} 13418 13419/* Map an ELF section header flag to its corresponding string. */ 13420typedef struct 13421{ 13422 char *flag_name; 13423 flagword flag_value; 13424} elf_flags_to_name_table; 13425 13426static elf_flags_to_name_table elf_flags_to_names [] = 13427{ 13428 { "SHF_WRITE", SHF_WRITE }, 13429 { "SHF_ALLOC", SHF_ALLOC }, 13430 { "SHF_EXECINSTR", SHF_EXECINSTR }, 13431 { "SHF_MERGE", SHF_MERGE }, 13432 { "SHF_STRINGS", SHF_STRINGS }, 13433 { "SHF_INFO_LINK", SHF_INFO_LINK}, 13434 { "SHF_LINK_ORDER", SHF_LINK_ORDER}, 13435 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, 13436 { "SHF_GROUP", SHF_GROUP }, 13437 { "SHF_TLS", SHF_TLS }, 13438 { "SHF_MASKOS", SHF_MASKOS }, 13439 { "SHF_EXCLUDE", SHF_EXCLUDE }, 13440}; 13441 13442/* Returns TRUE if the section is to be included, otherwise FALSE. */ 13443bfd_boolean 13444bfd_elf_lookup_section_flags (struct bfd_link_info *info, 13445 struct flag_info *flaginfo, 13446 asection *section) 13447{ 13448 const bfd_vma sh_flags = elf_section_flags (section); 13449 13450 if (!flaginfo->flags_initialized) 13451 { 13452 bfd *obfd = info->output_bfd; 13453 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 13454 struct flag_info_list *tf = flaginfo->flag_list; 13455 int with_hex = 0; 13456 int without_hex = 0; 13457 13458 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) 13459 { 13460 unsigned i; 13461 flagword (*lookup) (char *); 13462 13463 lookup = bed->elf_backend_lookup_section_flags_hook; 13464 if (lookup != NULL) 13465 { 13466 flagword hexval = (*lookup) ((char *) tf->name); 13467 13468 if (hexval != 0) 13469 { 13470 if (tf->with == with_flags) 13471 with_hex |= hexval; 13472 else if (tf->with == without_flags) 13473 without_hex |= hexval; 13474 tf->valid = TRUE; 13475 continue; 13476 } 13477 } 13478 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) 13479 { 13480 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) 13481 { 13482 if (tf->with == with_flags) 13483 with_hex |= elf_flags_to_names[i].flag_value; 13484 else if (tf->with == without_flags) 13485 without_hex |= elf_flags_to_names[i].flag_value; 13486 tf->valid = TRUE; 13487 break; 13488 } 13489 } 13490 if (!tf->valid) 13491 { 13492 info->callbacks->einfo 13493 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); 13494 return FALSE; 13495 } 13496 } 13497 flaginfo->flags_initialized = TRUE; 13498 flaginfo->only_with_flags |= with_hex; 13499 flaginfo->not_with_flags |= without_hex; 13500 } 13501 13502 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) 13503 return FALSE; 13504 13505 if ((flaginfo->not_with_flags & sh_flags) != 0) 13506 return FALSE; 13507 13508 return TRUE; 13509} 13510 13511struct alloc_got_off_arg { 13512 bfd_vma gotoff; 13513 struct bfd_link_info *info; 13514}; 13515 13516/* We need a special top-level link routine to convert got reference counts 13517 to real got offsets. */ 13518 13519static bfd_boolean 13520elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) 13521{ 13522 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; 13523 bfd *obfd = gofarg->info->output_bfd; 13524 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 13525 13526 if (h->got.refcount > 0) 13527 { 13528 h->got.offset = gofarg->gotoff; 13529 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); 13530 } 13531 else 13532 h->got.offset = (bfd_vma) -1; 13533 13534 return TRUE; 13535} 13536 13537/* And an accompanying bit to work out final got entry offsets once 13538 we're done. Should be called from final_link. */ 13539 13540bfd_boolean 13541bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, 13542 struct bfd_link_info *info) 13543{ 13544 bfd *i; 13545 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13546 bfd_vma gotoff; 13547 struct alloc_got_off_arg gofarg; 13548 13549 BFD_ASSERT (abfd == info->output_bfd); 13550 13551 if (! is_elf_hash_table (info->hash)) 13552 return FALSE; 13553 13554 /* The GOT offset is relative to the .got section, but the GOT header is 13555 put into the .got.plt section, if the backend uses it. */ 13556 if (bed->want_got_plt) 13557 gotoff = 0; 13558 else 13559 gotoff = bed->got_header_size; 13560 13561 /* Do the local .got entries first. */ 13562 for (i = info->input_bfds; i; i = i->link.next) 13563 { 13564 bfd_signed_vma *local_got; 13565 size_t j, locsymcount; 13566 Elf_Internal_Shdr *symtab_hdr; 13567 13568 if (bfd_get_flavour (i) != bfd_target_elf_flavour) 13569 continue; 13570 13571 local_got = elf_local_got_refcounts (i); 13572 if (!local_got) 13573 continue; 13574 13575 symtab_hdr = &elf_tdata (i)->symtab_hdr; 13576 if (elf_bad_symtab (i)) 13577 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 13578 else 13579 locsymcount = symtab_hdr->sh_info; 13580 13581 for (j = 0; j < locsymcount; ++j) 13582 { 13583 if (local_got[j] > 0) 13584 { 13585 local_got[j] = gotoff; 13586 gotoff += bed->got_elt_size (abfd, info, NULL, i, j); 13587 } 13588 else 13589 local_got[j] = (bfd_vma) -1; 13590 } 13591 } 13592 13593 /* Then the global .got entries. .plt refcounts are handled by 13594 adjust_dynamic_symbol */ 13595 gofarg.gotoff = gotoff; 13596 gofarg.info = info; 13597 elf_link_hash_traverse (elf_hash_table (info), 13598 elf_gc_allocate_got_offsets, 13599 &gofarg); 13600 return TRUE; 13601} 13602 13603/* Many folk need no more in the way of final link than this, once 13604 got entry reference counting is enabled. */ 13605 13606bfd_boolean 13607bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) 13608{ 13609 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) 13610 return FALSE; 13611 13612 /* Invoke the regular ELF backend linker to do all the work. */ 13613 return bfd_elf_final_link (abfd, info); 13614} 13615 13616bfd_boolean 13617bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) 13618{ 13619 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; 13620 13621 if (rcookie->bad_symtab) 13622 rcookie->rel = rcookie->rels; 13623 13624 for (; rcookie->rel < rcookie->relend; rcookie->rel++) 13625 { 13626 unsigned long r_symndx; 13627 13628 if (! rcookie->bad_symtab) 13629 if (rcookie->rel->r_offset > offset) 13630 return FALSE; 13631 if (rcookie->rel->r_offset != offset) 13632 continue; 13633 13634 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; 13635 if (r_symndx == STN_UNDEF) 13636 return TRUE; 13637 13638 if (r_symndx >= rcookie->locsymcount 13639 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) 13640 { 13641 struct elf_link_hash_entry *h; 13642 13643 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; 13644 13645 while (h->root.type == bfd_link_hash_indirect 13646 || h->root.type == bfd_link_hash_warning) 13647 h = (struct elf_link_hash_entry *) h->root.u.i.link; 13648 13649 if ((h->root.type == bfd_link_hash_defined 13650 || h->root.type == bfd_link_hash_defweak) 13651 && (h->root.u.def.section->owner != rcookie->abfd 13652 || h->root.u.def.section->kept_section != NULL 13653 || discarded_section (h->root.u.def.section))) 13654 return TRUE; 13655 } 13656 else 13657 { 13658 /* It's not a relocation against a global symbol, 13659 but it could be a relocation against a local 13660 symbol for a discarded section. */ 13661 asection *isec; 13662 Elf_Internal_Sym *isym; 13663 13664 /* Need to: get the symbol; get the section. */ 13665 isym = &rcookie->locsyms[r_symndx]; 13666 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); 13667 if (isec != NULL 13668 && (isec->kept_section != NULL 13669 || discarded_section (isec))) 13670 return TRUE; 13671 } 13672 return FALSE; 13673 } 13674 return FALSE; 13675} 13676 13677/* Discard unneeded references to discarded sections. 13678 Returns -1 on error, 1 if any section's size was changed, 0 if 13679 nothing changed. This function assumes that the relocations are in 13680 sorted order, which is true for all known assemblers. */ 13681 13682int 13683bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) 13684{ 13685 struct elf_reloc_cookie cookie; 13686 asection *o; 13687 bfd *abfd; 13688 int changed = 0; 13689 13690 if (info->traditional_format 13691 || !is_elf_hash_table (info->hash)) 13692 return 0; 13693 13694 o = bfd_get_section_by_name (output_bfd, ".stab"); 13695 if (o != NULL) 13696 { 13697 asection *i; 13698 13699 for (i = o->map_head.s; i != NULL; i = i->map_head.s) 13700 { 13701 if (i->size == 0 13702 || i->reloc_count == 0 13703 || i->sec_info_type != SEC_INFO_TYPE_STABS) 13704 continue; 13705 13706 abfd = i->owner; 13707 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 13708 continue; 13709 13710 if (!init_reloc_cookie_for_section (&cookie, info, i)) 13711 return -1; 13712 13713 if (_bfd_discard_section_stabs (abfd, i, 13714 elf_section_data (i)->sec_info, 13715 bfd_elf_reloc_symbol_deleted_p, 13716 &cookie)) 13717 changed = 1; 13718 13719 fini_reloc_cookie_for_section (&cookie, i); 13720 } 13721 } 13722 13723 o = NULL; 13724 if (info->eh_frame_hdr_type != COMPACT_EH_HDR) 13725 o = bfd_get_section_by_name (output_bfd, ".eh_frame"); 13726 if (o != NULL) 13727 { 13728 asection *i; 13729 13730 for (i = o->map_head.s; i != NULL; i = i->map_head.s) 13731 { 13732 if (i->size == 0) 13733 continue; 13734 13735 abfd = i->owner; 13736 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 13737 continue; 13738 13739 if (!init_reloc_cookie_for_section (&cookie, info, i)) 13740 return -1; 13741 13742 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); 13743 if (_bfd_elf_discard_section_eh_frame (abfd, info, i, 13744 bfd_elf_reloc_symbol_deleted_p, 13745 &cookie)) 13746 changed = 1; 13747 13748 fini_reloc_cookie_for_section (&cookie, i); 13749 } 13750 } 13751 13752 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) 13753 { 13754 const struct elf_backend_data *bed; 13755 13756 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 13757 continue; 13758 13759 bed = get_elf_backend_data (abfd); 13760 13761 if (bed->elf_backend_discard_info != NULL) 13762 { 13763 if (!init_reloc_cookie (&cookie, info, abfd)) 13764 return -1; 13765 13766 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) 13767 changed = 1; 13768 13769 fini_reloc_cookie (&cookie, abfd); 13770 } 13771 } 13772 13773 if (info->eh_frame_hdr_type == COMPACT_EH_HDR) 13774 _bfd_elf_end_eh_frame_parsing (info); 13775 13776 if (info->eh_frame_hdr_type 13777 && !bfd_link_relocatable (info) 13778 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) 13779 changed = 1; 13780 13781 return changed; 13782} 13783 13784bfd_boolean 13785_bfd_elf_section_already_linked (bfd *abfd, 13786 asection *sec, 13787 struct bfd_link_info *info) 13788{ 13789 flagword flags; 13790 const char *name, *key; 13791 struct bfd_section_already_linked *l; 13792 struct bfd_section_already_linked_hash_entry *already_linked_list; 13793 13794 if (sec->output_section == bfd_abs_section_ptr) 13795 return FALSE; 13796 13797 flags = sec->flags; 13798 13799 /* Return if it isn't a linkonce section. A comdat group section 13800 also has SEC_LINK_ONCE set. */ 13801 if ((flags & SEC_LINK_ONCE) == 0) 13802 return FALSE; 13803 13804 /* Don't put group member sections on our list of already linked 13805 sections. They are handled as a group via their group section. */ 13806 if (elf_sec_group (sec) != NULL) 13807 return FALSE; 13808 13809 /* For a SHT_GROUP section, use the group signature as the key. */ 13810 name = sec->name; 13811 if ((flags & SEC_GROUP) != 0 13812 && elf_next_in_group (sec) != NULL 13813 && elf_group_name (elf_next_in_group (sec)) != NULL) 13814 key = elf_group_name (elf_next_in_group (sec)); 13815 else 13816 { 13817 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ 13818 if (CONST_STRNEQ (name, ".gnu.linkonce.") 13819 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) 13820 key++; 13821 else 13822 /* Must be a user linkonce section that doesn't follow gcc's 13823 naming convention. In this case we won't be matching 13824 single member groups. */ 13825 key = name; 13826 } 13827 13828 already_linked_list = bfd_section_already_linked_table_lookup (key); 13829 13830 for (l = already_linked_list->entry; l != NULL; l = l->next) 13831 { 13832 /* We may have 2 different types of sections on the list: group 13833 sections with a signature of <key> (<key> is some string), 13834 and linkonce sections named .gnu.linkonce.<type>.<key>. 13835 Match like sections. LTO plugin sections are an exception. 13836 They are always named .gnu.linkonce.t.<key> and match either 13837 type of section. */ 13838 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) 13839 && ((flags & SEC_GROUP) != 0 13840 || strcmp (name, l->sec->name) == 0)) 13841 || (l->sec->owner->flags & BFD_PLUGIN) != 0) 13842 { 13843 /* The section has already been linked. See if we should 13844 issue a warning. */ 13845 if (!_bfd_handle_already_linked (sec, l, info)) 13846 return FALSE; 13847 13848 if (flags & SEC_GROUP) 13849 { 13850 asection *first = elf_next_in_group (sec); 13851 asection *s = first; 13852 13853 while (s != NULL) 13854 { 13855 s->output_section = bfd_abs_section_ptr; 13856 /* Record which group discards it. */ 13857 s->kept_section = l->sec; 13858 s = elf_next_in_group (s); 13859 /* These lists are circular. */ 13860 if (s == first) 13861 break; 13862 } 13863 } 13864 13865 return TRUE; 13866 } 13867 } 13868 13869 /* A single member comdat group section may be discarded by a 13870 linkonce section and vice versa. */ 13871 if ((flags & SEC_GROUP) != 0) 13872 { 13873 asection *first = elf_next_in_group (sec); 13874 13875 if (first != NULL && elf_next_in_group (first) == first) 13876 /* Check this single member group against linkonce sections. */ 13877 for (l = already_linked_list->entry; l != NULL; l = l->next) 13878 if ((l->sec->flags & SEC_GROUP) == 0 13879 && bfd_elf_match_symbols_in_sections (l->sec, first, info)) 13880 { 13881 first->output_section = bfd_abs_section_ptr; 13882 first->kept_section = l->sec; 13883 sec->output_section = bfd_abs_section_ptr; 13884 break; 13885 } 13886 } 13887 else 13888 /* Check this linkonce section against single member groups. */ 13889 for (l = already_linked_list->entry; l != NULL; l = l->next) 13890 if (l->sec->flags & SEC_GROUP) 13891 { 13892 asection *first = elf_next_in_group (l->sec); 13893 13894 if (first != NULL 13895 && elf_next_in_group (first) == first 13896 && bfd_elf_match_symbols_in_sections (first, sec, info)) 13897 { 13898 sec->output_section = bfd_abs_section_ptr; 13899 sec->kept_section = first; 13900 break; 13901 } 13902 } 13903 13904 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' 13905 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 13906 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' 13907 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its 13908 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded 13909 but its `.gnu.linkonce.t.F' is discarded means we chose one-only 13910 `.gnu.linkonce.t.F' section from a different bfd not requiring any 13911 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. 13912 The reverse order cannot happen as there is never a bfd with only the 13913 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not 13914 matter as here were are looking only for cross-bfd sections. */ 13915 13916 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) 13917 for (l = already_linked_list->entry; l != NULL; l = l->next) 13918 if ((l->sec->flags & SEC_GROUP) == 0 13919 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) 13920 { 13921 if (abfd != l->sec->owner) 13922 sec->output_section = bfd_abs_section_ptr; 13923 break; 13924 } 13925 13926 /* This is the first section with this name. Record it. */ 13927 if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) 13928 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); 13929 return sec->output_section == bfd_abs_section_ptr; 13930} 13931 13932bfd_boolean 13933_bfd_elf_common_definition (Elf_Internal_Sym *sym) 13934{ 13935 return sym->st_shndx == SHN_COMMON; 13936} 13937 13938unsigned int 13939_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) 13940{ 13941 return SHN_COMMON; 13942} 13943 13944asection * 13945_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) 13946{ 13947 return bfd_com_section_ptr; 13948} 13949 13950bfd_vma 13951_bfd_elf_default_got_elt_size (bfd *abfd, 13952 struct bfd_link_info *info ATTRIBUTE_UNUSED, 13953 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, 13954 bfd *ibfd ATTRIBUTE_UNUSED, 13955 unsigned long symndx ATTRIBUTE_UNUSED) 13956{ 13957 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13958 return bed->s->arch_size / 8; 13959} 13960 13961/* Routines to support the creation of dynamic relocs. */ 13962 13963/* Returns the name of the dynamic reloc section associated with SEC. */ 13964 13965static const char * 13966get_dynamic_reloc_section_name (bfd * abfd, 13967 asection * sec, 13968 bfd_boolean is_rela) 13969{ 13970 char *name; 13971 const char *old_name = bfd_get_section_name (NULL, sec); 13972 const char *prefix = is_rela ? ".rela" : ".rel"; 13973 13974 if (old_name == NULL) 13975 return NULL; 13976 13977 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); 13978 sprintf (name, "%s%s", prefix, old_name); 13979 13980 return name; 13981} 13982 13983/* Returns the dynamic reloc section associated with SEC. 13984 If necessary compute the name of the dynamic reloc section based 13985 on SEC's name (looked up in ABFD's string table) and the setting 13986 of IS_RELA. */ 13987 13988asection * 13989_bfd_elf_get_dynamic_reloc_section (bfd * abfd, 13990 asection * sec, 13991 bfd_boolean is_rela) 13992{ 13993 asection * reloc_sec = elf_section_data (sec)->sreloc; 13994 13995 if (reloc_sec == NULL) 13996 { 13997 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); 13998 13999 if (name != NULL) 14000 { 14001 reloc_sec = bfd_get_linker_section (abfd, name); 14002 14003 if (reloc_sec != NULL) 14004 elf_section_data (sec)->sreloc = reloc_sec; 14005 } 14006 } 14007 14008 return reloc_sec; 14009} 14010 14011/* Returns the dynamic reloc section associated with SEC. If the 14012 section does not exist it is created and attached to the DYNOBJ 14013 bfd and stored in the SRELOC field of SEC's elf_section_data 14014 structure. 14015 14016 ALIGNMENT is the alignment for the newly created section and 14017 IS_RELA defines whether the name should be .rela.<SEC's name> 14018 or .rel.<SEC's name>. The section name is looked up in the 14019 string table associated with ABFD. */ 14020 14021asection * 14022_bfd_elf_make_dynamic_reloc_section (asection *sec, 14023 bfd *dynobj, 14024 unsigned int alignment, 14025 bfd *abfd, 14026 bfd_boolean is_rela) 14027{ 14028 asection * reloc_sec = elf_section_data (sec)->sreloc; 14029 14030 if (reloc_sec == NULL) 14031 { 14032 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); 14033 14034 if (name == NULL) 14035 return NULL; 14036 14037 reloc_sec = bfd_get_linker_section (dynobj, name); 14038 14039 if (reloc_sec == NULL) 14040 { 14041 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY 14042 | SEC_IN_MEMORY | SEC_LINKER_CREATED); 14043 if ((sec->flags & SEC_ALLOC) != 0) 14044 flags |= SEC_ALLOC | SEC_LOAD; 14045 14046 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); 14047 if (reloc_sec != NULL) 14048 { 14049 /* _bfd_elf_get_sec_type_attr chooses a section type by 14050 name. Override as it may be wrong, eg. for a user 14051 section named "auto" we'll get ".relauto" which is 14052 seen to be a .rela section. */ 14053 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; 14054 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) 14055 reloc_sec = NULL; 14056 } 14057 } 14058 14059 elf_section_data (sec)->sreloc = reloc_sec; 14060 } 14061 14062 return reloc_sec; 14063} 14064 14065/* Copy the ELF symbol type and other attributes for a linker script 14066 assignment from HSRC to HDEST. Generally this should be treated as 14067 if we found a strong non-dynamic definition for HDEST (except that 14068 ld ignores multiple definition errors). */ 14069void 14070_bfd_elf_copy_link_hash_symbol_type (bfd *abfd, 14071 struct bfd_link_hash_entry *hdest, 14072 struct bfd_link_hash_entry *hsrc) 14073{ 14074 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; 14075 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; 14076 Elf_Internal_Sym isym; 14077 14078 ehdest->type = ehsrc->type; 14079 ehdest->target_internal = ehsrc->target_internal; 14080 14081 isym.st_other = ehsrc->other; 14082 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); 14083} 14084 14085/* Append a RELA relocation REL to section S in BFD. */ 14086 14087void 14088elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) 14089{ 14090 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14091 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); 14092 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); 14093 bed->s->swap_reloca_out (abfd, rel, loc); 14094} 14095 14096/* Append a REL relocation REL to section S in BFD. */ 14097 14098void 14099elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) 14100{ 14101 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14102 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); 14103 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); 14104 bed->s->swap_reloc_out (abfd, rel, loc); 14105} 14106