1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
32 /* This struct is used to pass information to routines called via
33 elf_link_hash_traverse which must return failure. */
35 struct elf_info_failed
37 struct bfd_link_info *info;
41 /* This structure is used to pass information to
42 _bfd_elf_link_find_version_dependencies. */
44 struct elf_find_verdep_info
46 /* General link information. */
47 struct bfd_link_info *info;
48 /* The number of dependencies. */
50 /* Whether we had a failure. */
54 static bfd_boolean _bfd_elf_fix_symbol_flags
55 (struct elf_link_hash_entry *, struct elf_info_failed *);
58 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59 unsigned long r_symndx,
62 if (r_symndx >= cookie->locsymcount
63 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
65 struct elf_link_hash_entry *h;
67 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
69 while (h->root.type == bfd_link_hash_indirect
70 || h->root.type == bfd_link_hash_warning)
71 h = (struct elf_link_hash_entry *) h->root.u.i.link;
73 if ((h->root.type == bfd_link_hash_defined
74 || h->root.type == bfd_link_hash_defweak)
75 && discarded_section (h->root.u.def.section))
76 return h->root.u.def.section;
82 /* It's not a relocation against a global symbol,
83 but it could be a relocation against a local
84 symbol for a discarded section. */
86 Elf_Internal_Sym *isym;
88 /* Need to: get the symbol; get the section. */
89 isym = &cookie->locsyms[r_symndx];
90 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
92 && discard ? discarded_section (isec) : 1)
98 /* Define a symbol in a dynamic linkage section. */
100 struct elf_link_hash_entry *
101 _bfd_elf_define_linkage_sym (bfd *abfd,
102 struct bfd_link_info *info,
106 struct elf_link_hash_entry *h;
107 struct bfd_link_hash_entry *bh;
108 const struct elf_backend_data *bed;
110 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
113 /* Zap symbol defined in an as-needed lib that wasn't linked.
114 This is a symptom of a larger problem: Absolute symbols
115 defined in shared libraries can't be overridden, because we
116 lose the link to the bfd which is via the symbol section. */
117 h->root.type = bfd_link_hash_new;
121 bed = get_elf_backend_data (abfd);
122 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
123 sec, 0, NULL, FALSE, bed->collect,
126 h = (struct elf_link_hash_entry *) bh;
129 h->root.linker_def = 1;
130 h->type = STT_OBJECT;
131 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
134 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
139 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
143 struct elf_link_hash_entry *h;
144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
145 struct elf_link_hash_table *htab = elf_hash_table (info);
147 /* This function may be called more than once. */
148 s = bfd_get_linker_section (abfd, ".got");
152 flags = bed->dynamic_sec_flags;
154 s = bfd_make_section_anyway_with_flags (abfd,
155 (bed->rela_plts_and_copies_p
156 ? ".rela.got" : ".rel.got"),
157 (bed->dynamic_sec_flags
160 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
166 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 if (bed->want_got_plt)
172 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
174 || !bfd_set_section_alignment (abfd, s,
175 bed->s->log_file_align))
180 /* The first bit of the global offset table is the header. */
181 s->size += bed->got_header_size;
183 if (bed->want_got_sym)
185 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 (or .got.plt) section. We don't do this in the linker script
187 because we don't want to define the symbol if we are not creating
188 a global offset table. */
189 h = _bfd_elf_define_linkage_sym (abfd, info, s,
190 "_GLOBAL_OFFSET_TABLE_");
191 elf_hash_table (info)->hgot = h;
199 /* Create a strtab to hold the dynamic symbol names. */
201 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
203 struct elf_link_hash_table *hash_table;
205 hash_table = elf_hash_table (info);
206 if (hash_table->dynobj == NULL)
208 /* We may not set dynobj, an input file holding linker created
209 dynamic sections to abfd, which may be a dynamic object with
210 its own dynamic sections. We need to find a normal input file
211 to hold linker created sections if possible. */
212 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
215 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
216 if ((ibfd->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
222 hash_table->dynobj = abfd;
225 if (hash_table->dynstr == NULL)
227 hash_table->dynstr = _bfd_elf_strtab_init ();
228 if (hash_table->dynstr == NULL)
234 /* Create some sections which will be filled in with dynamic linking
235 information. ABFD is an input file which requires dynamic sections
236 to be created. The dynamic sections take up virtual memory space
237 when the final executable is run, so we need to create them before
238 addresses are assigned to the output sections. We work out the
239 actual contents and size of these sections later. */
242 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
246 const struct elf_backend_data *bed;
247 struct elf_link_hash_entry *h;
249 if (! is_elf_hash_table (info->hash))
252 if (elf_hash_table (info)->dynamic_sections_created)
255 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
258 abfd = elf_hash_table (info)->dynobj;
259 bed = get_elf_backend_data (abfd);
261 flags = bed->dynamic_sec_flags;
263 /* A dynamically linked executable has a .interp section, but a
264 shared library does not. */
265 if (bfd_link_executable (info) && !info->nointerp)
267 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
268 flags | SEC_READONLY);
273 /* Create sections to hold version informations. These are removed
274 if they are not needed. */
275 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
276 flags | SEC_READONLY);
278 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
281 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
282 flags | SEC_READONLY);
284 || ! bfd_set_section_alignment (abfd, s, 1))
287 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
288 flags | SEC_READONLY);
290 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
293 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
294 flags | SEC_READONLY);
296 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
298 elf_hash_table (info)->dynsym = s;
300 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
301 flags | SEC_READONLY);
305 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
310 /* The special symbol _DYNAMIC is always set to the start of the
311 .dynamic section. We could set _DYNAMIC in a linker script, but we
312 only want to define it if we are, in fact, creating a .dynamic
313 section. We don't want to define it if there is no .dynamic
314 section, since on some ELF platforms the start up code examines it
315 to decide how to initialize the process. */
316 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
317 elf_hash_table (info)->hdynamic = h;
323 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
324 flags | SEC_READONLY);
326 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
328 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
331 if (info->emit_gnu_hash)
333 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
334 flags | SEC_READONLY);
336 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
339 4 32-bit words followed by variable count of 64-bit words, then
340 variable count of 32-bit words. */
341 if (bed->s->arch_size == 64)
342 elf_section_data (s)->this_hdr.sh_entsize = 0;
344 elf_section_data (s)->this_hdr.sh_entsize = 4;
347 /* Let the backend create the rest of the sections. This lets the
348 backend set the right flags. The backend will normally create
349 the .got and .plt sections. */
350 if (bed->elf_backend_create_dynamic_sections == NULL
351 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
354 elf_hash_table (info)->dynamic_sections_created = TRUE;
359 /* Create dynamic sections when linking against a dynamic object. */
362 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
364 flagword flags, pltflags;
365 struct elf_link_hash_entry *h;
367 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
368 struct elf_link_hash_table *htab = elf_hash_table (info);
370 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
371 .rel[a].bss sections. */
372 flags = bed->dynamic_sec_flags;
375 if (bed->plt_not_loaded)
376 /* We do not clear SEC_ALLOC here because we still want the OS to
377 allocate space for the section; it's just that there's nothing
378 to read in from the object file. */
379 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
381 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
382 if (bed->plt_readonly)
383 pltflags |= SEC_READONLY;
385 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
387 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
391 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
393 if (bed->want_plt_sym)
395 h = _bfd_elf_define_linkage_sym (abfd, info, s,
396 "_PROCEDURE_LINKAGE_TABLE_");
397 elf_hash_table (info)->hplt = h;
402 s = bfd_make_section_anyway_with_flags (abfd,
403 (bed->rela_plts_and_copies_p
404 ? ".rela.plt" : ".rel.plt"),
405 flags | SEC_READONLY);
407 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
411 if (! _bfd_elf_create_got_section (abfd, info))
414 if (bed->want_dynbss)
416 /* The .dynbss section is a place to put symbols which are defined
417 by dynamic objects, are referenced by regular objects, and are
418 not functions. We must allocate space for them in the process
419 image and use a R_*_COPY reloc to tell the dynamic linker to
420 initialize them at run time. The linker script puts the .dynbss
421 section into the .bss section of the final image. */
422 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
423 (SEC_ALLOC | SEC_LINKER_CREATED));
427 /* The .rel[a].bss section holds copy relocs. This section is not
428 normally needed. We need to create it here, though, so that the
429 linker will map it to an output section. We can't just create it
430 only if we need it, because we will not know whether we need it
431 until we have seen all the input files, and the first time the
432 main linker code calls BFD after examining all the input files
433 (size_dynamic_sections) the input sections have already been
434 mapped to the output sections. If the section turns out not to
435 be needed, we can discard it later. We will never need this
436 section when generating a shared object, since they do not use
438 if (! bfd_link_pic (info))
440 s = bfd_make_section_anyway_with_flags (abfd,
441 (bed->rela_plts_and_copies_p
442 ? ".rela.bss" : ".rel.bss"),
443 flags | SEC_READONLY);
445 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
453 /* Record a new dynamic symbol. We record the dynamic symbols as we
454 read the input files, since we need to have a list of all of them
455 before we can determine the final sizes of the output sections.
456 Note that we may actually call this function even though we are not
457 going to output any dynamic symbols; in some cases we know that a
458 symbol should be in the dynamic symbol table, but only if there is
462 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
463 struct elf_link_hash_entry *h)
465 if (h->dynindx == -1)
467 struct elf_strtab_hash *dynstr;
472 /* XXX: The ABI draft says the linker must turn hidden and
473 internal symbols into STB_LOCAL symbols when producing the
474 DSO. However, if ld.so honors st_other in the dynamic table,
475 this would not be necessary. */
476 switch (ELF_ST_VISIBILITY (h->other))
480 if (h->root.type != bfd_link_hash_undefined
481 && h->root.type != bfd_link_hash_undefweak)
484 if (!elf_hash_table (info)->is_relocatable_executable)
492 h->dynindx = elf_hash_table (info)->dynsymcount;
493 ++elf_hash_table (info)->dynsymcount;
495 dynstr = elf_hash_table (info)->dynstr;
498 /* Create a strtab to hold the dynamic symbol names. */
499 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
504 /* We don't put any version information in the dynamic string
506 name = h->root.root.string;
507 p = strchr (name, ELF_VER_CHR);
509 /* We know that the p points into writable memory. In fact,
510 there are only a few symbols that have read-only names, being
511 those like _GLOBAL_OFFSET_TABLE_ that are created specially
512 by the backends. Most symbols will have names pointing into
513 an ELF string table read from a file, or to objalloc memory. */
516 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
521 if (indx == (bfd_size_type) -1)
523 h->dynstr_index = indx;
529 /* Mark a symbol dynamic. */
532 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
533 struct elf_link_hash_entry *h,
534 Elf_Internal_Sym *sym)
536 struct bfd_elf_dynamic_list *d = info->dynamic_list;
538 /* It may be called more than once on the same H. */
539 if(h->dynamic || bfd_link_relocatable (info))
542 if ((info->dynamic_data
543 && (h->type == STT_OBJECT
544 || h->type == STT_COMMON
546 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
547 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
549 && h->root.type == bfd_link_hash_new
550 && (*d->match) (&d->head, NULL, h->root.root.string)))
554 /* Record an assignment to a symbol made by a linker script. We need
555 this in case some dynamic object refers to this symbol. */
558 bfd_elf_record_link_assignment (bfd *output_bfd,
559 struct bfd_link_info *info,
564 struct elf_link_hash_entry *h, *hv;
565 struct elf_link_hash_table *htab;
566 const struct elf_backend_data *bed;
568 if (!is_elf_hash_table (info->hash))
571 htab = elf_hash_table (info);
572 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
576 if (h->versioned == unknown)
578 /* Set versioned if symbol version is unknown. */
579 char *version = strrchr (name, ELF_VER_CHR);
582 if (version > name && version[-1] != ELF_VER_CHR)
583 h->versioned = versioned_hidden;
585 h->versioned = versioned;
589 switch (h->root.type)
591 case bfd_link_hash_defined:
592 case bfd_link_hash_defweak:
593 case bfd_link_hash_common:
595 case bfd_link_hash_undefweak:
596 case bfd_link_hash_undefined:
597 /* Since we're defining the symbol, don't let it seem to have not
598 been defined. record_dynamic_symbol and size_dynamic_sections
599 may depend on this. */
600 h->root.type = bfd_link_hash_new;
601 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
602 bfd_link_repair_undef_list (&htab->root);
604 case bfd_link_hash_new:
605 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
608 case bfd_link_hash_indirect:
609 /* We had a versioned symbol in a dynamic library. We make the
610 the versioned symbol point to this one. */
611 bed = get_elf_backend_data (output_bfd);
613 while (hv->root.type == bfd_link_hash_indirect
614 || hv->root.type == bfd_link_hash_warning)
615 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
616 /* We don't need to update h->root.u since linker will set them
618 h->root.type = bfd_link_hash_undefined;
619 hv->root.type = bfd_link_hash_indirect;
620 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
621 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
623 case bfd_link_hash_warning:
628 /* If this symbol is being provided by the linker script, and it is
629 currently defined by a dynamic object, but not by a regular
630 object, then mark it as undefined so that the generic linker will
631 force the correct value. */
635 h->root.type = bfd_link_hash_undefined;
637 /* If this symbol is not being provided by the linker script, and it is
638 currently defined by a dynamic object, but not by a regular object,
639 then clear out any version information because the symbol will not be
640 associated with the dynamic object any more. */
644 h->verinfo.verdef = NULL;
650 bed = get_elf_backend_data (output_bfd);
651 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
652 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
653 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
656 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
658 if (!bfd_link_relocatable (info)
660 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
661 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
666 || bfd_link_dll (info)
667 || elf_hash_table (info)->is_relocatable_executable)
670 if (! bfd_elf_link_record_dynamic_symbol (info, h))
673 /* If this is a weak defined symbol, and we know a corresponding
674 real symbol from the same dynamic object, make sure the real
675 symbol is also made into a dynamic symbol. */
676 if (h->u.weakdef != NULL
677 && h->u.weakdef->dynindx == -1)
679 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
687 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
688 success, and 2 on a failure caused by attempting to record a symbol
689 in a discarded section, eg. a discarded link-once section symbol. */
692 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
697 struct elf_link_local_dynamic_entry *entry;
698 struct elf_link_hash_table *eht;
699 struct elf_strtab_hash *dynstr;
700 unsigned long dynstr_index;
702 Elf_External_Sym_Shndx eshndx;
703 char esym[sizeof (Elf64_External_Sym)];
705 if (! is_elf_hash_table (info->hash))
708 /* See if the entry exists already. */
709 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
710 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
713 amt = sizeof (*entry);
714 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
718 /* Go find the symbol, so that we can find it's name. */
719 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
720 1, input_indx, &entry->isym, esym, &eshndx))
722 bfd_release (input_bfd, entry);
726 if (entry->isym.st_shndx != SHN_UNDEF
727 && entry->isym.st_shndx < SHN_LORESERVE)
731 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
732 if (s == NULL || bfd_is_abs_section (s->output_section))
734 /* We can still bfd_release here as nothing has done another
735 bfd_alloc. We can't do this later in this function. */
736 bfd_release (input_bfd, entry);
741 name = (bfd_elf_string_from_elf_section
742 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
743 entry->isym.st_name));
745 dynstr = elf_hash_table (info)->dynstr;
748 /* Create a strtab to hold the dynamic symbol names. */
749 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
754 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
755 if (dynstr_index == (unsigned long) -1)
757 entry->isym.st_name = dynstr_index;
759 eht = elf_hash_table (info);
761 entry->next = eht->dynlocal;
762 eht->dynlocal = entry;
763 entry->input_bfd = input_bfd;
764 entry->input_indx = input_indx;
767 /* Whatever binding the symbol had before, it's now local. */
769 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
771 /* The dynindx will be set at the end of size_dynamic_sections. */
776 /* Return the dynindex of a local dynamic symbol. */
779 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
783 struct elf_link_local_dynamic_entry *e;
785 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
786 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
791 /* This function is used to renumber the dynamic symbols, if some of
792 them are removed because they are marked as local. This is called
793 via elf_link_hash_traverse. */
796 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
799 size_t *count = (size_t *) data;
804 if (h->dynindx != -1)
805 h->dynindx = ++(*count);
811 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
812 STB_LOCAL binding. */
815 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
818 size_t *count = (size_t *) data;
820 if (!h->forced_local)
823 if (h->dynindx != -1)
824 h->dynindx = ++(*count);
829 /* Return true if the dynamic symbol for a given section should be
830 omitted when creating a shared library. */
832 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
833 struct bfd_link_info *info,
836 struct elf_link_hash_table *htab;
839 switch (elf_section_data (p)->this_hdr.sh_type)
843 /* If sh_type is yet undecided, assume it could be
844 SHT_PROGBITS/SHT_NOBITS. */
846 htab = elf_hash_table (info);
847 if (p == htab->tls_sec)
850 if (htab->text_index_section != NULL)
851 return p != htab->text_index_section && p != htab->data_index_section;
853 return (htab->dynobj != NULL
854 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
855 && ip->output_section == p);
857 /* There shouldn't be section relative relocations
858 against any other section. */
864 /* Assign dynsym indices. In a shared library we generate a section
865 symbol for each output section, which come first. Next come symbols
866 which have been forced to local binding. Then all of the back-end
867 allocated local dynamic syms, followed by the rest of the global
871 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
872 struct bfd_link_info *info,
873 unsigned long *section_sym_count)
875 unsigned long dynsymcount = 0;
877 if (bfd_link_pic (info)
878 || elf_hash_table (info)->is_relocatable_executable)
880 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
882 for (p = output_bfd->sections; p ; p = p->next)
883 if ((p->flags & SEC_EXCLUDE) == 0
884 && (p->flags & SEC_ALLOC) != 0
885 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
886 elf_section_data (p)->dynindx = ++dynsymcount;
888 elf_section_data (p)->dynindx = 0;
890 *section_sym_count = dynsymcount;
892 elf_link_hash_traverse (elf_hash_table (info),
893 elf_link_renumber_local_hash_table_dynsyms,
896 if (elf_hash_table (info)->dynlocal)
898 struct elf_link_local_dynamic_entry *p;
899 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
900 p->dynindx = ++dynsymcount;
903 elf_link_hash_traverse (elf_hash_table (info),
904 elf_link_renumber_hash_table_dynsyms,
907 /* There is an unused NULL entry at the head of the table which
908 we must account for in our count. We always create the dynsym
909 section, even if it is empty, with dynamic sections. */
910 if (elf_hash_table (info)->dynamic_sections_created)
913 elf_hash_table (info)->dynsymcount = dynsymcount;
917 /* Merge st_other field. */
920 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
921 const Elf_Internal_Sym *isym, asection *sec,
922 bfd_boolean definition, bfd_boolean dynamic)
924 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
926 /* If st_other has a processor-specific meaning, specific
927 code might be needed here. */
928 if (bed->elf_backend_merge_symbol_attribute)
929 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
934 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
935 unsigned hvis = ELF_ST_VISIBILITY (h->other);
937 /* Keep the most constraining visibility. Leave the remainder
938 of the st_other field to elf_backend_merge_symbol_attribute. */
939 if (symvis - 1 < hvis - 1)
940 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
943 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
944 && (sec->flags & SEC_READONLY) == 0)
945 h->protected_def = 1;
948 /* This function is called when we want to merge a new symbol with an
949 existing symbol. It handles the various cases which arise when we
950 find a definition in a dynamic object, or when there is already a
951 definition in a dynamic object. The new symbol is described by
952 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
953 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
954 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
955 of an old common symbol. We set OVERRIDE if the old symbol is
956 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
957 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
958 to change. By OK to change, we mean that we shouldn't warn if the
959 type or size does change. */
962 _bfd_elf_merge_symbol (bfd *abfd,
963 struct bfd_link_info *info,
965 Elf_Internal_Sym *sym,
968 struct elf_link_hash_entry **sym_hash,
970 bfd_boolean *pold_weak,
971 unsigned int *pold_alignment,
973 bfd_boolean *override,
974 bfd_boolean *type_change_ok,
975 bfd_boolean *size_change_ok,
976 bfd_boolean *matched)
978 asection *sec, *oldsec;
979 struct elf_link_hash_entry *h;
980 struct elf_link_hash_entry *hi;
981 struct elf_link_hash_entry *flip;
984 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
985 bfd_boolean newweak, oldweak, newfunc, oldfunc;
986 const struct elf_backend_data *bed;
993 bind = ELF_ST_BIND (sym->st_info);
995 if (! bfd_is_und_section (sec))
996 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
998 h = ((struct elf_link_hash_entry *)
999 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1004 bed = get_elf_backend_data (abfd);
1006 /* NEW_VERSION is the symbol version of the new symbol. */
1007 if (h->versioned != unversioned)
1009 /* Symbol version is unknown or versioned. */
1010 new_version = strrchr (name, ELF_VER_CHR);
1013 if (h->versioned == unknown)
1015 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1016 h->versioned = versioned_hidden;
1018 h->versioned = versioned;
1021 if (new_version[0] == '\0')
1025 h->versioned = unversioned;
1030 /* For merging, we only care about real symbols. But we need to make
1031 sure that indirect symbol dynamic flags are updated. */
1033 while (h->root.type == bfd_link_hash_indirect
1034 || h->root.type == bfd_link_hash_warning)
1035 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1039 if (hi == h || h->root.type == bfd_link_hash_new)
1043 /* OLD_HIDDEN is true if the existing symbol is only visible
1044 to the symbol with the same symbol version. NEW_HIDDEN is
1045 true if the new symbol is only visible to the symbol with
1046 the same symbol version. */
1047 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1048 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1049 if (!old_hidden && !new_hidden)
1050 /* The new symbol matches the existing symbol if both
1055 /* OLD_VERSION is the symbol version of the existing
1059 if (h->versioned >= versioned)
1060 old_version = strrchr (h->root.root.string,
1065 /* The new symbol matches the existing symbol if they
1066 have the same symbol version. */
1067 *matched = (old_version == new_version
1068 || (old_version != NULL
1069 && new_version != NULL
1070 && strcmp (old_version, new_version) == 0));
1075 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1080 switch (h->root.type)
1085 case bfd_link_hash_undefined:
1086 case bfd_link_hash_undefweak:
1087 oldbfd = h->root.u.undef.abfd;
1090 case bfd_link_hash_defined:
1091 case bfd_link_hash_defweak:
1092 oldbfd = h->root.u.def.section->owner;
1093 oldsec = h->root.u.def.section;
1096 case bfd_link_hash_common:
1097 oldbfd = h->root.u.c.p->section->owner;
1098 oldsec = h->root.u.c.p->section;
1100 *pold_alignment = h->root.u.c.p->alignment_power;
1103 if (poldbfd && *poldbfd == NULL)
1106 /* Differentiate strong and weak symbols. */
1107 newweak = bind == STB_WEAK;
1108 oldweak = (h->root.type == bfd_link_hash_defweak
1109 || h->root.type == bfd_link_hash_undefweak);
1111 *pold_weak = oldweak;
1113 /* This code is for coping with dynamic objects, and is only useful
1114 if we are doing an ELF link. */
1115 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1118 /* We have to check it for every instance since the first few may be
1119 references and not all compilers emit symbol type for undefined
1121 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1123 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1124 respectively, is from a dynamic object. */
1126 newdyn = (abfd->flags & DYNAMIC) != 0;
1128 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1129 syms and defined syms in dynamic libraries respectively.
1130 ref_dynamic on the other hand can be set for a symbol defined in
1131 a dynamic library, and def_dynamic may not be set; When the
1132 definition in a dynamic lib is overridden by a definition in the
1133 executable use of the symbol in the dynamic lib becomes a
1134 reference to the executable symbol. */
1137 if (bfd_is_und_section (sec))
1139 if (bind != STB_WEAK)
1141 h->ref_dynamic_nonweak = 1;
1142 hi->ref_dynamic_nonweak = 1;
1147 /* Update the existing symbol only if they match. */
1150 hi->dynamic_def = 1;
1154 /* If we just created the symbol, mark it as being an ELF symbol.
1155 Other than that, there is nothing to do--there is no merge issue
1156 with a newly defined symbol--so we just return. */
1158 if (h->root.type == bfd_link_hash_new)
1164 /* In cases involving weak versioned symbols, we may wind up trying
1165 to merge a symbol with itself. Catch that here, to avoid the
1166 confusion that results if we try to override a symbol with
1167 itself. The additional tests catch cases like
1168 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1169 dynamic object, which we do want to handle here. */
1171 && (newweak || oldweak)
1172 && ((abfd->flags & DYNAMIC) == 0
1173 || !h->def_regular))
1178 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1179 else if (oldsec != NULL)
1181 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1182 indices used by MIPS ELF. */
1183 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1186 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1187 respectively, appear to be a definition rather than reference. */
1189 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1191 olddef = (h->root.type != bfd_link_hash_undefined
1192 && h->root.type != bfd_link_hash_undefweak
1193 && h->root.type != bfd_link_hash_common);
1195 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1196 respectively, appear to be a function. */
1198 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1199 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1201 oldfunc = (h->type != STT_NOTYPE
1202 && bed->is_function_type (h->type));
1204 /* When we try to create a default indirect symbol from the dynamic
1205 definition with the default version, we skip it if its type and
1206 the type of existing regular definition mismatch. */
1207 if (pold_alignment == NULL
1211 && (((olddef || h->root.type == bfd_link_hash_common)
1212 && ELF_ST_TYPE (sym->st_info) != h->type
1213 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1214 && h->type != STT_NOTYPE
1215 && !(newfunc && oldfunc))
1217 && ((h->type == STT_GNU_IFUNC)
1218 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
1224 /* Check TLS symbols. We don't check undefined symbols introduced
1225 by "ld -u" which have no type (and oldbfd NULL), and we don't
1226 check symbols from plugins because they also have no type. */
1228 && (oldbfd->flags & BFD_PLUGIN) == 0
1229 && (abfd->flags & BFD_PLUGIN) == 0
1230 && ELF_ST_TYPE (sym->st_info) != h->type
1231 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1234 bfd_boolean ntdef, tdef;
1235 asection *ntsec, *tsec;
1237 if (h->type == STT_TLS)
1257 (*_bfd_error_handler)
1258 (_("%s: TLS definition in %B section %A "
1259 "mismatches non-TLS definition in %B section %A"),
1260 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1261 else if (!tdef && !ntdef)
1262 (*_bfd_error_handler)
1263 (_("%s: TLS reference in %B "
1264 "mismatches non-TLS reference in %B"),
1265 tbfd, ntbfd, h->root.root.string);
1267 (*_bfd_error_handler)
1268 (_("%s: TLS definition in %B section %A "
1269 "mismatches non-TLS reference in %B"),
1270 tbfd, tsec, ntbfd, h->root.root.string);
1272 (*_bfd_error_handler)
1273 (_("%s: TLS reference in %B "
1274 "mismatches non-TLS definition in %B section %A"),
1275 tbfd, ntbfd, ntsec, h->root.root.string);
1277 bfd_set_error (bfd_error_bad_value);
1281 /* If the old symbol has non-default visibility, we ignore the new
1282 definition from a dynamic object. */
1284 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1285 && !bfd_is_und_section (sec))
1288 /* Make sure this symbol is dynamic. */
1290 hi->ref_dynamic = 1;
1291 /* A protected symbol has external availability. Make sure it is
1292 recorded as dynamic.
1294 FIXME: Should we check type and size for protected symbol? */
1295 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1296 return bfd_elf_link_record_dynamic_symbol (info, h);
1301 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1304 /* If the new symbol with non-default visibility comes from a
1305 relocatable file and the old definition comes from a dynamic
1306 object, we remove the old definition. */
1307 if (hi->root.type == bfd_link_hash_indirect)
1309 /* Handle the case where the old dynamic definition is
1310 default versioned. We need to copy the symbol info from
1311 the symbol with default version to the normal one if it
1312 was referenced before. */
1315 hi->root.type = h->root.type;
1316 h->root.type = bfd_link_hash_indirect;
1317 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1319 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1320 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1322 /* If the new symbol is hidden or internal, completely undo
1323 any dynamic link state. */
1324 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1325 h->forced_local = 0;
1332 /* FIXME: Should we check type and size for protected symbol? */
1342 /* If the old symbol was undefined before, then it will still be
1343 on the undefs list. If the new symbol is undefined or
1344 common, we can't make it bfd_link_hash_new here, because new
1345 undefined or common symbols will be added to the undefs list
1346 by _bfd_generic_link_add_one_symbol. Symbols may not be
1347 added twice to the undefs list. Also, if the new symbol is
1348 undefweak then we don't want to lose the strong undef. */
1349 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1351 h->root.type = bfd_link_hash_undefined;
1352 h->root.u.undef.abfd = abfd;
1356 h->root.type = bfd_link_hash_new;
1357 h->root.u.undef.abfd = NULL;
1360 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1362 /* If the new symbol is hidden or internal, completely undo
1363 any dynamic link state. */
1364 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1365 h->forced_local = 0;
1371 /* FIXME: Should we check type and size for protected symbol? */
1377 /* If a new weak symbol definition comes from a regular file and the
1378 old symbol comes from a dynamic library, we treat the new one as
1379 strong. Similarly, an old weak symbol definition from a regular
1380 file is treated as strong when the new symbol comes from a dynamic
1381 library. Further, an old weak symbol from a dynamic library is
1382 treated as strong if the new symbol is from a dynamic library.
1383 This reflects the way glibc's ld.so works.
1385 Do this before setting *type_change_ok or *size_change_ok so that
1386 we warn properly when dynamic library symbols are overridden. */
1388 if (newdef && !newdyn && olddyn)
1390 if (olddef && newdyn)
1393 /* Allow changes between different types of function symbol. */
1394 if (newfunc && oldfunc)
1395 *type_change_ok = TRUE;
1397 /* It's OK to change the type if either the existing symbol or the
1398 new symbol is weak. A type change is also OK if the old symbol
1399 is undefined and the new symbol is defined. */
1404 && h->root.type == bfd_link_hash_undefined))
1405 *type_change_ok = TRUE;
1407 /* It's OK to change the size if either the existing symbol or the
1408 new symbol is weak, or if the old symbol is undefined. */
1411 || h->root.type == bfd_link_hash_undefined)
1412 *size_change_ok = TRUE;
1414 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1415 symbol, respectively, appears to be a common symbol in a dynamic
1416 object. If a symbol appears in an uninitialized section, and is
1417 not weak, and is not a function, then it may be a common symbol
1418 which was resolved when the dynamic object was created. We want
1419 to treat such symbols specially, because they raise special
1420 considerations when setting the symbol size: if the symbol
1421 appears as a common symbol in a regular object, and the size in
1422 the regular object is larger, we must make sure that we use the
1423 larger size. This problematic case can always be avoided in C,
1424 but it must be handled correctly when using Fortran shared
1427 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1428 likewise for OLDDYNCOMMON and OLDDEF.
1430 Note that this test is just a heuristic, and that it is quite
1431 possible to have an uninitialized symbol in a shared object which
1432 is really a definition, rather than a common symbol. This could
1433 lead to some minor confusion when the symbol really is a common
1434 symbol in some regular object. However, I think it will be
1440 && (sec->flags & SEC_ALLOC) != 0
1441 && (sec->flags & SEC_LOAD) == 0
1444 newdyncommon = TRUE;
1446 newdyncommon = FALSE;
1450 && h->root.type == bfd_link_hash_defined
1452 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1453 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1456 olddyncommon = TRUE;
1458 olddyncommon = FALSE;
1460 /* We now know everything about the old and new symbols. We ask the
1461 backend to check if we can merge them. */
1462 if (bed->merge_symbol != NULL)
1464 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1469 /* If both the old and the new symbols look like common symbols in a
1470 dynamic object, set the size of the symbol to the larger of the
1475 && sym->st_size != h->size)
1477 /* Since we think we have two common symbols, issue a multiple
1478 common warning if desired. Note that we only warn if the
1479 size is different. If the size is the same, we simply let
1480 the old symbol override the new one as normally happens with
1481 symbols defined in dynamic objects. */
1483 if (! ((*info->callbacks->multiple_common)
1484 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1487 if (sym->st_size > h->size)
1488 h->size = sym->st_size;
1490 *size_change_ok = TRUE;
1493 /* If we are looking at a dynamic object, and we have found a
1494 definition, we need to see if the symbol was already defined by
1495 some other object. If so, we want to use the existing
1496 definition, and we do not want to report a multiple symbol
1497 definition error; we do this by clobbering *PSEC to be
1498 bfd_und_section_ptr.
1500 We treat a common symbol as a definition if the symbol in the
1501 shared library is a function, since common symbols always
1502 represent variables; this can cause confusion in principle, but
1503 any such confusion would seem to indicate an erroneous program or
1504 shared library. We also permit a common symbol in a regular
1505 object to override a weak symbol in a shared object. A common
1506 symbol in executable also overrides a symbol in a shared object. */
1511 || (h->root.type == bfd_link_hash_common
1514 || (!olddyn && bfd_link_executable (info))))))
1518 newdyncommon = FALSE;
1520 *psec = sec = bfd_und_section_ptr;
1521 *size_change_ok = TRUE;
1523 /* If we get here when the old symbol is a common symbol, then
1524 we are explicitly letting it override a weak symbol or
1525 function in a dynamic object, and we don't want to warn about
1526 a type change. If the old symbol is a defined symbol, a type
1527 change warning may still be appropriate. */
1529 if (h->root.type == bfd_link_hash_common)
1530 *type_change_ok = TRUE;
1533 /* Handle the special case of an old common symbol merging with a
1534 new symbol which looks like a common symbol in a shared object.
1535 We change *PSEC and *PVALUE to make the new symbol look like a
1536 common symbol, and let _bfd_generic_link_add_one_symbol do the
1540 && h->root.type == bfd_link_hash_common)
1544 newdyncommon = FALSE;
1545 *pvalue = sym->st_size;
1546 *psec = sec = bed->common_section (oldsec);
1547 *size_change_ok = TRUE;
1550 /* Skip weak definitions of symbols that are already defined. */
1551 if (newdef && olddef && newweak)
1553 /* Don't skip new non-IR weak syms. */
1554 if (!(oldbfd != NULL
1555 && (oldbfd->flags & BFD_PLUGIN) != 0
1556 && (abfd->flags & BFD_PLUGIN) == 0))
1562 /* Merge st_other. If the symbol already has a dynamic index,
1563 but visibility says it should not be visible, turn it into a
1565 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1566 if (h->dynindx != -1)
1567 switch (ELF_ST_VISIBILITY (h->other))
1571 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1576 /* If the old symbol is from a dynamic object, and the new symbol is
1577 a definition which is not from a dynamic object, then the new
1578 symbol overrides the old symbol. Symbols from regular files
1579 always take precedence over symbols from dynamic objects, even if
1580 they are defined after the dynamic object in the link.
1582 As above, we again permit a common symbol in a regular object to
1583 override a definition in a shared object if the shared object
1584 symbol is a function or is weak. */
1589 || (bfd_is_com_section (sec)
1590 && (oldweak || oldfunc)))
1595 /* Change the hash table entry to undefined, and let
1596 _bfd_generic_link_add_one_symbol do the right thing with the
1599 h->root.type = bfd_link_hash_undefined;
1600 h->root.u.undef.abfd = h->root.u.def.section->owner;
1601 *size_change_ok = TRUE;
1604 olddyncommon = FALSE;
1606 /* We again permit a type change when a common symbol may be
1607 overriding a function. */
1609 if (bfd_is_com_section (sec))
1613 /* If a common symbol overrides a function, make sure
1614 that it isn't defined dynamically nor has type
1617 h->type = STT_NOTYPE;
1619 *type_change_ok = TRUE;
1622 if (hi->root.type == bfd_link_hash_indirect)
1625 /* This union may have been set to be non-NULL when this symbol
1626 was seen in a dynamic object. We must force the union to be
1627 NULL, so that it is correct for a regular symbol. */
1628 h->verinfo.vertree = NULL;
1631 /* Handle the special case of a new common symbol merging with an
1632 old symbol that looks like it might be a common symbol defined in
1633 a shared object. Note that we have already handled the case in
1634 which a new common symbol should simply override the definition
1635 in the shared library. */
1638 && bfd_is_com_section (sec)
1641 /* It would be best if we could set the hash table entry to a
1642 common symbol, but we don't know what to use for the section
1643 or the alignment. */
1644 if (! ((*info->callbacks->multiple_common)
1645 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1648 /* If the presumed common symbol in the dynamic object is
1649 larger, pretend that the new symbol has its size. */
1651 if (h->size > *pvalue)
1654 /* We need to remember the alignment required by the symbol
1655 in the dynamic object. */
1656 BFD_ASSERT (pold_alignment);
1657 *pold_alignment = h->root.u.def.section->alignment_power;
1660 olddyncommon = FALSE;
1662 h->root.type = bfd_link_hash_undefined;
1663 h->root.u.undef.abfd = h->root.u.def.section->owner;
1665 *size_change_ok = TRUE;
1666 *type_change_ok = TRUE;
1668 if (hi->root.type == bfd_link_hash_indirect)
1671 h->verinfo.vertree = NULL;
1676 /* Handle the case where we had a versioned symbol in a dynamic
1677 library and now find a definition in a normal object. In this
1678 case, we make the versioned symbol point to the normal one. */
1679 flip->root.type = h->root.type;
1680 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1681 h->root.type = bfd_link_hash_indirect;
1682 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1683 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1687 flip->ref_dynamic = 1;
1694 /* This function is called to create an indirect symbol from the
1695 default for the symbol with the default version if needed. The
1696 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1697 set DYNSYM if the new indirect symbol is dynamic. */
1700 _bfd_elf_add_default_symbol (bfd *abfd,
1701 struct bfd_link_info *info,
1702 struct elf_link_hash_entry *h,
1704 Elf_Internal_Sym *sym,
1708 bfd_boolean *dynsym)
1710 bfd_boolean type_change_ok;
1711 bfd_boolean size_change_ok;
1714 struct elf_link_hash_entry *hi;
1715 struct bfd_link_hash_entry *bh;
1716 const struct elf_backend_data *bed;
1717 bfd_boolean collect;
1718 bfd_boolean dynamic;
1719 bfd_boolean override;
1721 size_t len, shortlen;
1723 bfd_boolean matched;
1725 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1728 /* If this symbol has a version, and it is the default version, we
1729 create an indirect symbol from the default name to the fully
1730 decorated name. This will cause external references which do not
1731 specify a version to be bound to this version of the symbol. */
1732 p = strchr (name, ELF_VER_CHR);
1733 if (h->versioned == unknown)
1737 h->versioned = unversioned;
1742 if (p[1] != ELF_VER_CHR)
1744 h->versioned = versioned_hidden;
1748 h->versioned = versioned;
1753 /* PR ld/19073: We may see an unversioned definition after the
1759 bed = get_elf_backend_data (abfd);
1760 collect = bed->collect;
1761 dynamic = (abfd->flags & DYNAMIC) != 0;
1763 shortlen = p - name;
1764 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1765 if (shortname == NULL)
1767 memcpy (shortname, name, shortlen);
1768 shortname[shortlen] = '\0';
1770 /* We are going to create a new symbol. Merge it with any existing
1771 symbol with this name. For the purposes of the merge, act as
1772 though we were defining the symbol we just defined, although we
1773 actually going to define an indirect symbol. */
1774 type_change_ok = FALSE;
1775 size_change_ok = FALSE;
1778 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1779 &hi, poldbfd, NULL, NULL, &skip, &override,
1780 &type_change_ok, &size_change_ok, &matched))
1788 /* Add the default symbol if not performing a relocatable link. */
1789 if (! bfd_link_relocatable (info))
1792 if (! (_bfd_generic_link_add_one_symbol
1793 (info, abfd, shortname, BSF_INDIRECT,
1794 bfd_ind_section_ptr,
1795 0, name, FALSE, collect, &bh)))
1797 hi = (struct elf_link_hash_entry *) bh;
1802 /* In this case the symbol named SHORTNAME is overriding the
1803 indirect symbol we want to add. We were planning on making
1804 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1805 is the name without a version. NAME is the fully versioned
1806 name, and it is the default version.
1808 Overriding means that we already saw a definition for the
1809 symbol SHORTNAME in a regular object, and it is overriding
1810 the symbol defined in the dynamic object.
1812 When this happens, we actually want to change NAME, the
1813 symbol we just added, to refer to SHORTNAME. This will cause
1814 references to NAME in the shared object to become references
1815 to SHORTNAME in the regular object. This is what we expect
1816 when we override a function in a shared object: that the
1817 references in the shared object will be mapped to the
1818 definition in the regular object. */
1820 while (hi->root.type == bfd_link_hash_indirect
1821 || hi->root.type == bfd_link_hash_warning)
1822 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1824 h->root.type = bfd_link_hash_indirect;
1825 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1829 hi->ref_dynamic = 1;
1833 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1838 /* Now set HI to H, so that the following code will set the
1839 other fields correctly. */
1843 /* Check if HI is a warning symbol. */
1844 if (hi->root.type == bfd_link_hash_warning)
1845 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1847 /* If there is a duplicate definition somewhere, then HI may not
1848 point to an indirect symbol. We will have reported an error to
1849 the user in that case. */
1851 if (hi->root.type == bfd_link_hash_indirect)
1853 struct elf_link_hash_entry *ht;
1855 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1856 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1858 /* A reference to the SHORTNAME symbol from a dynamic library
1859 will be satisfied by the versioned symbol at runtime. In
1860 effect, we have a reference to the versioned symbol. */
1861 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1862 hi->dynamic_def |= ht->dynamic_def;
1864 /* See if the new flags lead us to realize that the symbol must
1870 if (! bfd_link_executable (info)
1877 if (hi->ref_regular)
1883 /* We also need to define an indirection from the nondefault version
1887 len = strlen (name);
1888 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1889 if (shortname == NULL)
1891 memcpy (shortname, name, shortlen);
1892 memcpy (shortname + shortlen, p + 1, len - shortlen);
1894 /* Once again, merge with any existing symbol. */
1895 type_change_ok = FALSE;
1896 size_change_ok = FALSE;
1898 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1899 &hi, poldbfd, NULL, NULL, &skip, &override,
1900 &type_change_ok, &size_change_ok, &matched))
1908 /* Here SHORTNAME is a versioned name, so we don't expect to see
1909 the type of override we do in the case above unless it is
1910 overridden by a versioned definition. */
1911 if (hi->root.type != bfd_link_hash_defined
1912 && hi->root.type != bfd_link_hash_defweak)
1913 (*_bfd_error_handler)
1914 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1920 if (! (_bfd_generic_link_add_one_symbol
1921 (info, abfd, shortname, BSF_INDIRECT,
1922 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1924 hi = (struct elf_link_hash_entry *) bh;
1926 /* If there is a duplicate definition somewhere, then HI may not
1927 point to an indirect symbol. We will have reported an error
1928 to the user in that case. */
1930 if (hi->root.type == bfd_link_hash_indirect)
1932 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1933 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1934 hi->dynamic_def |= h->dynamic_def;
1936 /* See if the new flags lead us to realize that the symbol
1942 if (! bfd_link_executable (info)
1948 if (hi->ref_regular)
1958 /* This routine is used to export all defined symbols into the dynamic
1959 symbol table. It is called via elf_link_hash_traverse. */
1962 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1964 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1966 /* Ignore indirect symbols. These are added by the versioning code. */
1967 if (h->root.type == bfd_link_hash_indirect)
1970 /* Ignore this if we won't export it. */
1971 if (!eif->info->export_dynamic && !h->dynamic)
1974 if (h->dynindx == -1
1975 && (h->def_regular || h->ref_regular)
1976 && ! bfd_hide_sym_by_version (eif->info->version_info,
1977 h->root.root.string))
1979 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1989 /* Look through the symbols which are defined in other shared
1990 libraries and referenced here. Update the list of version
1991 dependencies. This will be put into the .gnu.version_r section.
1992 This function is called via elf_link_hash_traverse. */
1995 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1998 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1999 Elf_Internal_Verneed *t;
2000 Elf_Internal_Vernaux *a;
2003 /* We only care about symbols defined in shared objects with version
2008 || h->verinfo.verdef == NULL
2009 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2010 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2013 /* See if we already know about this version. */
2014 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2018 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2021 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2022 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2028 /* This is a new version. Add it to tree we are building. */
2033 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2036 rinfo->failed = TRUE;
2040 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2041 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2042 elf_tdata (rinfo->info->output_bfd)->verref = t;
2046 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2049 rinfo->failed = TRUE;
2053 /* Note that we are copying a string pointer here, and testing it
2054 above. If bfd_elf_string_from_elf_section is ever changed to
2055 discard the string data when low in memory, this will have to be
2057 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2059 a->vna_flags = h->verinfo.verdef->vd_flags;
2060 a->vna_nextptr = t->vn_auxptr;
2062 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2065 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2072 /* Figure out appropriate versions for all the symbols. We may not
2073 have the version number script until we have read all of the input
2074 files, so until that point we don't know which symbols should be
2075 local. This function is called via elf_link_hash_traverse. */
2078 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2080 struct elf_info_failed *sinfo;
2081 struct bfd_link_info *info;
2082 const struct elf_backend_data *bed;
2083 struct elf_info_failed eif;
2087 sinfo = (struct elf_info_failed *) data;
2090 /* Fix the symbol flags. */
2093 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2096 sinfo->failed = TRUE;
2100 /* We only need version numbers for symbols defined in regular
2102 if (!h->def_regular)
2105 bed = get_elf_backend_data (info->output_bfd);
2106 p = strchr (h->root.root.string, ELF_VER_CHR);
2107 if (p != NULL && h->verinfo.vertree == NULL)
2109 struct bfd_elf_version_tree *t;
2112 if (*p == ELF_VER_CHR)
2115 /* If there is no version string, we can just return out. */
2119 /* Look for the version. If we find it, it is no longer weak. */
2120 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2122 if (strcmp (t->name, p) == 0)
2126 struct bfd_elf_version_expr *d;
2128 len = p - h->root.root.string;
2129 alc = (char *) bfd_malloc (len);
2132 sinfo->failed = TRUE;
2135 memcpy (alc, h->root.root.string, len - 1);
2136 alc[len - 1] = '\0';
2137 if (alc[len - 2] == ELF_VER_CHR)
2138 alc[len - 2] = '\0';
2140 h->verinfo.vertree = t;
2144 if (t->globals.list != NULL)
2145 d = (*t->match) (&t->globals, NULL, alc);
2147 /* See if there is anything to force this symbol to
2149 if (d == NULL && t->locals.list != NULL)
2151 d = (*t->match) (&t->locals, NULL, alc);
2154 && ! info->export_dynamic)
2155 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2163 /* If we are building an application, we need to create a
2164 version node for this version. */
2165 if (t == NULL && bfd_link_executable (info))
2167 struct bfd_elf_version_tree **pp;
2170 /* If we aren't going to export this symbol, we don't need
2171 to worry about it. */
2172 if (h->dynindx == -1)
2176 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2179 sinfo->failed = TRUE;
2184 t->name_indx = (unsigned int) -1;
2188 /* Don't count anonymous version tag. */
2189 if (sinfo->info->version_info != NULL
2190 && sinfo->info->version_info->vernum == 0)
2192 for (pp = &sinfo->info->version_info;
2196 t->vernum = version_index;
2200 h->verinfo.vertree = t;
2204 /* We could not find the version for a symbol when
2205 generating a shared archive. Return an error. */
2206 (*_bfd_error_handler)
2207 (_("%B: version node not found for symbol %s"),
2208 info->output_bfd, h->root.root.string);
2209 bfd_set_error (bfd_error_bad_value);
2210 sinfo->failed = TRUE;
2215 /* If we don't have a version for this symbol, see if we can find
2217 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2222 = bfd_find_version_for_sym (sinfo->info->version_info,
2223 h->root.root.string, &hide);
2224 if (h->verinfo.vertree != NULL && hide)
2225 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2231 /* Read and swap the relocs from the section indicated by SHDR. This
2232 may be either a REL or a RELA section. The relocations are
2233 translated into RELA relocations and stored in INTERNAL_RELOCS,
2234 which should have already been allocated to contain enough space.
2235 The EXTERNAL_RELOCS are a buffer where the external form of the
2236 relocations should be stored.
2238 Returns FALSE if something goes wrong. */
2241 elf_link_read_relocs_from_section (bfd *abfd,
2243 Elf_Internal_Shdr *shdr,
2244 void *external_relocs,
2245 Elf_Internal_Rela *internal_relocs)
2247 const struct elf_backend_data *bed;
2248 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2249 const bfd_byte *erela;
2250 const bfd_byte *erelaend;
2251 Elf_Internal_Rela *irela;
2252 Elf_Internal_Shdr *symtab_hdr;
2255 /* Position ourselves at the start of the section. */
2256 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2259 /* Read the relocations. */
2260 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2263 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2264 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2266 bed = get_elf_backend_data (abfd);
2268 /* Convert the external relocations to the internal format. */
2269 if (shdr->sh_entsize == bed->s->sizeof_rel)
2270 swap_in = bed->s->swap_reloc_in;
2271 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2272 swap_in = bed->s->swap_reloca_in;
2275 bfd_set_error (bfd_error_wrong_format);
2279 erela = (const bfd_byte *) external_relocs;
2280 erelaend = erela + shdr->sh_size;
2281 irela = internal_relocs;
2282 while (erela < erelaend)
2286 (*swap_in) (abfd, erela, irela);
2287 r_symndx = ELF32_R_SYM (irela->r_info);
2288 if (bed->s->arch_size == 64)
2292 if ((size_t) r_symndx >= nsyms)
2294 (*_bfd_error_handler)
2295 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2296 " for offset 0x%lx in section `%A'"),
2298 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2299 bfd_set_error (bfd_error_bad_value);
2303 else if (r_symndx != STN_UNDEF)
2305 (*_bfd_error_handler)
2306 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2307 " when the object file has no symbol table"),
2309 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2310 bfd_set_error (bfd_error_bad_value);
2313 irela += bed->s->int_rels_per_ext_rel;
2314 erela += shdr->sh_entsize;
2320 /* Read and swap the relocs for a section O. They may have been
2321 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2322 not NULL, they are used as buffers to read into. They are known to
2323 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2324 the return value is allocated using either malloc or bfd_alloc,
2325 according to the KEEP_MEMORY argument. If O has two relocation
2326 sections (both REL and RELA relocations), then the REL_HDR
2327 relocations will appear first in INTERNAL_RELOCS, followed by the
2328 RELA_HDR relocations. */
2331 _bfd_elf_link_read_relocs (bfd *abfd,
2333 void *external_relocs,
2334 Elf_Internal_Rela *internal_relocs,
2335 bfd_boolean keep_memory)
2337 void *alloc1 = NULL;
2338 Elf_Internal_Rela *alloc2 = NULL;
2339 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2340 struct bfd_elf_section_data *esdo = elf_section_data (o);
2341 Elf_Internal_Rela *internal_rela_relocs;
2343 if (esdo->relocs != NULL)
2344 return esdo->relocs;
2346 if (o->reloc_count == 0)
2349 if (internal_relocs == NULL)
2353 size = o->reloc_count;
2354 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2356 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2358 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2359 if (internal_relocs == NULL)
2363 if (external_relocs == NULL)
2365 bfd_size_type size = 0;
2368 size += esdo->rel.hdr->sh_size;
2370 size += esdo->rela.hdr->sh_size;
2372 alloc1 = bfd_malloc (size);
2375 external_relocs = alloc1;
2378 internal_rela_relocs = internal_relocs;
2381 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2385 external_relocs = (((bfd_byte *) external_relocs)
2386 + esdo->rel.hdr->sh_size);
2387 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2388 * bed->s->int_rels_per_ext_rel);
2392 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2394 internal_rela_relocs)))
2397 /* Cache the results for next time, if we can. */
2399 esdo->relocs = internal_relocs;
2404 /* Don't free alloc2, since if it was allocated we are passing it
2405 back (under the name of internal_relocs). */
2407 return internal_relocs;
2415 bfd_release (abfd, alloc2);
2422 /* Compute the size of, and allocate space for, REL_HDR which is the
2423 section header for a section containing relocations for O. */
2426 _bfd_elf_link_size_reloc_section (bfd *abfd,
2427 struct bfd_elf_section_reloc_data *reldata)
2429 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2431 /* That allows us to calculate the size of the section. */
2432 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2434 /* The contents field must last into write_object_contents, so we
2435 allocate it with bfd_alloc rather than malloc. Also since we
2436 cannot be sure that the contents will actually be filled in,
2437 we zero the allocated space. */
2438 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2439 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2442 if (reldata->hashes == NULL && reldata->count)
2444 struct elf_link_hash_entry **p;
2446 p = ((struct elf_link_hash_entry **)
2447 bfd_zmalloc (reldata->count * sizeof (*p)));
2451 reldata->hashes = p;
2457 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2458 originated from the section given by INPUT_REL_HDR) to the
2462 _bfd_elf_link_output_relocs (bfd *output_bfd,
2463 asection *input_section,
2464 Elf_Internal_Shdr *input_rel_hdr,
2465 Elf_Internal_Rela *internal_relocs,
2466 struct elf_link_hash_entry **rel_hash
2469 Elf_Internal_Rela *irela;
2470 Elf_Internal_Rela *irelaend;
2472 struct bfd_elf_section_reloc_data *output_reldata;
2473 asection *output_section;
2474 const struct elf_backend_data *bed;
2475 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2476 struct bfd_elf_section_data *esdo;
2478 output_section = input_section->output_section;
2480 bed = get_elf_backend_data (output_bfd);
2481 esdo = elf_section_data (output_section);
2482 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2484 output_reldata = &esdo->rel;
2485 swap_out = bed->s->swap_reloc_out;
2487 else if (esdo->rela.hdr
2488 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2490 output_reldata = &esdo->rela;
2491 swap_out = bed->s->swap_reloca_out;
2495 (*_bfd_error_handler)
2496 (_("%B: relocation size mismatch in %B section %A"),
2497 output_bfd, input_section->owner, input_section);
2498 bfd_set_error (bfd_error_wrong_format);
2502 erel = output_reldata->hdr->contents;
2503 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2504 irela = internal_relocs;
2505 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2506 * bed->s->int_rels_per_ext_rel);
2507 while (irela < irelaend)
2509 (*swap_out) (output_bfd, irela, erel);
2510 irela += bed->s->int_rels_per_ext_rel;
2511 erel += input_rel_hdr->sh_entsize;
2514 /* Bump the counter, so that we know where to add the next set of
2516 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2521 /* Make weak undefined symbols in PIE dynamic. */
2524 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2525 struct elf_link_hash_entry *h)
2527 if (bfd_link_pie (info)
2529 && h->root.type == bfd_link_hash_undefweak)
2530 return bfd_elf_link_record_dynamic_symbol (info, h);
2535 /* Fix up the flags for a symbol. This handles various cases which
2536 can only be fixed after all the input files are seen. This is
2537 currently called by both adjust_dynamic_symbol and
2538 assign_sym_version, which is unnecessary but perhaps more robust in
2539 the face of future changes. */
2542 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2543 struct elf_info_failed *eif)
2545 const struct elf_backend_data *bed;
2547 /* If this symbol was mentioned in a non-ELF file, try to set
2548 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2549 permit a non-ELF file to correctly refer to a symbol defined in
2550 an ELF dynamic object. */
2553 while (h->root.type == bfd_link_hash_indirect)
2554 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2556 if (h->root.type != bfd_link_hash_defined
2557 && h->root.type != bfd_link_hash_defweak)
2560 h->ref_regular_nonweak = 1;
2564 if (h->root.u.def.section->owner != NULL
2565 && (bfd_get_flavour (h->root.u.def.section->owner)
2566 == bfd_target_elf_flavour))
2569 h->ref_regular_nonweak = 1;
2575 if (h->dynindx == -1
2579 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2588 /* Unfortunately, NON_ELF is only correct if the symbol
2589 was first seen in a non-ELF file. Fortunately, if the symbol
2590 was first seen in an ELF file, we're probably OK unless the
2591 symbol was defined in a non-ELF file. Catch that case here.
2592 FIXME: We're still in trouble if the symbol was first seen in
2593 a dynamic object, and then later in a non-ELF regular object. */
2594 if ((h->root.type == bfd_link_hash_defined
2595 || h->root.type == bfd_link_hash_defweak)
2597 && (h->root.u.def.section->owner != NULL
2598 ? (bfd_get_flavour (h->root.u.def.section->owner)
2599 != bfd_target_elf_flavour)
2600 : (bfd_is_abs_section (h->root.u.def.section)
2601 && !h->def_dynamic)))
2605 /* Backend specific symbol fixup. */
2606 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2607 if (bed->elf_backend_fixup_symbol
2608 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2611 /* If this is a final link, and the symbol was defined as a common
2612 symbol in a regular object file, and there was no definition in
2613 any dynamic object, then the linker will have allocated space for
2614 the symbol in a common section but the DEF_REGULAR
2615 flag will not have been set. */
2616 if (h->root.type == bfd_link_hash_defined
2620 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2623 /* If -Bsymbolic was used (which means to bind references to global
2624 symbols to the definition within the shared object), and this
2625 symbol was defined in a regular object, then it actually doesn't
2626 need a PLT entry. Likewise, if the symbol has non-default
2627 visibility. If the symbol has hidden or internal visibility, we
2628 will force it local. */
2630 && bfd_link_pic (eif->info)
2631 && is_elf_hash_table (eif->info->hash)
2632 && (SYMBOLIC_BIND (eif->info, h)
2633 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2636 bfd_boolean force_local;
2638 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2639 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2640 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2643 /* If a weak undefined symbol has non-default visibility, we also
2644 hide it from the dynamic linker. */
2645 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2646 && h->root.type == bfd_link_hash_undefweak)
2647 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2649 /* If this is a weak defined symbol in a dynamic object, and we know
2650 the real definition in the dynamic object, copy interesting flags
2651 over to the real definition. */
2652 if (h->u.weakdef != NULL)
2654 /* If the real definition is defined by a regular object file,
2655 don't do anything special. See the longer description in
2656 _bfd_elf_adjust_dynamic_symbol, below. */
2657 if (h->u.weakdef->def_regular)
2658 h->u.weakdef = NULL;
2661 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2663 while (h->root.type == bfd_link_hash_indirect)
2664 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2666 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2667 || h->root.type == bfd_link_hash_defweak);
2668 BFD_ASSERT (weakdef->def_dynamic);
2669 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2670 || weakdef->root.type == bfd_link_hash_defweak);
2671 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2678 /* Make the backend pick a good value for a dynamic symbol. This is
2679 called via elf_link_hash_traverse, and also calls itself
2683 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2685 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2687 const struct elf_backend_data *bed;
2689 if (! is_elf_hash_table (eif->info->hash))
2692 /* Ignore indirect symbols. These are added by the versioning code. */
2693 if (h->root.type == bfd_link_hash_indirect)
2696 /* Fix the symbol flags. */
2697 if (! _bfd_elf_fix_symbol_flags (h, eif))
2700 /* If this symbol does not require a PLT entry, and it is not
2701 defined by a dynamic object, or is not referenced by a regular
2702 object, ignore it. We do have to handle a weak defined symbol,
2703 even if no regular object refers to it, if we decided to add it
2704 to the dynamic symbol table. FIXME: Do we normally need to worry
2705 about symbols which are defined by one dynamic object and
2706 referenced by another one? */
2708 && h->type != STT_GNU_IFUNC
2712 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2714 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2718 /* If we've already adjusted this symbol, don't do it again. This
2719 can happen via a recursive call. */
2720 if (h->dynamic_adjusted)
2723 /* Don't look at this symbol again. Note that we must set this
2724 after checking the above conditions, because we may look at a
2725 symbol once, decide not to do anything, and then get called
2726 recursively later after REF_REGULAR is set below. */
2727 h->dynamic_adjusted = 1;
2729 /* If this is a weak definition, and we know a real definition, and
2730 the real symbol is not itself defined by a regular object file,
2731 then get a good value for the real definition. We handle the
2732 real symbol first, for the convenience of the backend routine.
2734 Note that there is a confusing case here. If the real definition
2735 is defined by a regular object file, we don't get the real symbol
2736 from the dynamic object, but we do get the weak symbol. If the
2737 processor backend uses a COPY reloc, then if some routine in the
2738 dynamic object changes the real symbol, we will not see that
2739 change in the corresponding weak symbol. This is the way other
2740 ELF linkers work as well, and seems to be a result of the shared
2743 I will clarify this issue. Most SVR4 shared libraries define the
2744 variable _timezone and define timezone as a weak synonym. The
2745 tzset call changes _timezone. If you write
2746 extern int timezone;
2748 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2749 you might expect that, since timezone is a synonym for _timezone,
2750 the same number will print both times. However, if the processor
2751 backend uses a COPY reloc, then actually timezone will be copied
2752 into your process image, and, since you define _timezone
2753 yourself, _timezone will not. Thus timezone and _timezone will
2754 wind up at different memory locations. The tzset call will set
2755 _timezone, leaving timezone unchanged. */
2757 if (h->u.weakdef != NULL)
2759 /* If we get to this point, there is an implicit reference to
2760 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2761 h->u.weakdef->ref_regular = 1;
2763 /* Ensure that the backend adjust_dynamic_symbol function sees
2764 H->U.WEAKDEF before H by recursively calling ourselves. */
2765 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2769 /* If a symbol has no type and no size and does not require a PLT
2770 entry, then we are probably about to do the wrong thing here: we
2771 are probably going to create a COPY reloc for an empty object.
2772 This case can arise when a shared object is built with assembly
2773 code, and the assembly code fails to set the symbol type. */
2775 && h->type == STT_NOTYPE
2777 (*_bfd_error_handler)
2778 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2779 h->root.root.string);
2781 dynobj = elf_hash_table (eif->info)->dynobj;
2782 bed = get_elf_backend_data (dynobj);
2784 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2793 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2797 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2798 struct elf_link_hash_entry *h,
2801 unsigned int power_of_two;
2803 asection *sec = h->root.u.def.section;
2805 /* The section aligment of definition is the maximum alignment
2806 requirement of symbols defined in the section. Since we don't
2807 know the symbol alignment requirement, we start with the
2808 maximum alignment and check low bits of the symbol address
2809 for the minimum alignment. */
2810 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2811 mask = ((bfd_vma) 1 << power_of_two) - 1;
2812 while ((h->root.u.def.value & mask) != 0)
2818 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2821 /* Adjust the section alignment if needed. */
2822 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2827 /* We make sure that the symbol will be aligned properly. */
2828 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2830 /* Define the symbol as being at this point in DYNBSS. */
2831 h->root.u.def.section = dynbss;
2832 h->root.u.def.value = dynbss->size;
2834 /* Increment the size of DYNBSS to make room for the symbol. */
2835 dynbss->size += h->size;
2837 /* No error if extern_protected_data is true. */
2838 if (h->protected_def
2839 && (!info->extern_protected_data
2840 || (info->extern_protected_data < 0
2841 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2842 info->callbacks->einfo
2843 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2844 h->root.root.string);
2849 /* Adjust all external symbols pointing into SEC_MERGE sections
2850 to reflect the object merging within the sections. */
2853 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2857 if ((h->root.type == bfd_link_hash_defined
2858 || h->root.type == bfd_link_hash_defweak)
2859 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2860 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2862 bfd *output_bfd = (bfd *) data;
2864 h->root.u.def.value =
2865 _bfd_merged_section_offset (output_bfd,
2866 &h->root.u.def.section,
2867 elf_section_data (sec)->sec_info,
2868 h->root.u.def.value);
2874 /* Returns false if the symbol referred to by H should be considered
2875 to resolve local to the current module, and true if it should be
2876 considered to bind dynamically. */
2879 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2880 struct bfd_link_info *info,
2881 bfd_boolean not_local_protected)
2883 bfd_boolean binding_stays_local_p;
2884 const struct elf_backend_data *bed;
2885 struct elf_link_hash_table *hash_table;
2890 while (h->root.type == bfd_link_hash_indirect
2891 || h->root.type == bfd_link_hash_warning)
2892 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2894 /* If it was forced local, then clearly it's not dynamic. */
2895 if (h->dynindx == -1)
2897 if (h->forced_local)
2900 /* Identify the cases where name binding rules say that a
2901 visible symbol resolves locally. */
2902 binding_stays_local_p = (bfd_link_executable (info)
2903 || SYMBOLIC_BIND (info, h));
2905 switch (ELF_ST_VISIBILITY (h->other))
2912 hash_table = elf_hash_table (info);
2913 if (!is_elf_hash_table (hash_table))
2916 bed = get_elf_backend_data (hash_table->dynobj);
2918 /* Proper resolution for function pointer equality may require
2919 that these symbols perhaps be resolved dynamically, even though
2920 we should be resolving them to the current module. */
2921 if (!not_local_protected || !bed->is_function_type (h->type))
2922 binding_stays_local_p = TRUE;
2929 /* If it isn't defined locally, then clearly it's dynamic. */
2930 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2933 /* Otherwise, the symbol is dynamic if binding rules don't tell
2934 us that it remains local. */
2935 return !binding_stays_local_p;
2938 /* Return true if the symbol referred to by H should be considered
2939 to resolve local to the current module, and false otherwise. Differs
2940 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2941 undefined symbols. The two functions are virtually identical except
2942 for the place where forced_local and dynindx == -1 are tested. If
2943 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2944 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2945 the symbol is local only for defined symbols.
2946 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2947 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2948 treatment of undefined weak symbols. For those that do not make
2949 undefined weak symbols dynamic, both functions may return false. */
2952 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2953 struct bfd_link_info *info,
2954 bfd_boolean local_protected)
2956 const struct elf_backend_data *bed;
2957 struct elf_link_hash_table *hash_table;
2959 /* If it's a local sym, of course we resolve locally. */
2963 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2964 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2965 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2968 /* Common symbols that become definitions don't get the DEF_REGULAR
2969 flag set, so test it first, and don't bail out. */
2970 if (ELF_COMMON_DEF_P (h))
2972 /* If we don't have a definition in a regular file, then we can't
2973 resolve locally. The sym is either undefined or dynamic. */
2974 else if (!h->def_regular)
2977 /* Forced local symbols resolve locally. */
2978 if (h->forced_local)
2981 /* As do non-dynamic symbols. */
2982 if (h->dynindx == -1)
2985 /* At this point, we know the symbol is defined and dynamic. In an
2986 executable it must resolve locally, likewise when building symbolic
2987 shared libraries. */
2988 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
2991 /* Now deal with defined dynamic symbols in shared libraries. Ones
2992 with default visibility might not resolve locally. */
2993 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2996 hash_table = elf_hash_table (info);
2997 if (!is_elf_hash_table (hash_table))
3000 bed = get_elf_backend_data (hash_table->dynobj);
3002 /* If extern_protected_data is false, STV_PROTECTED non-function
3003 symbols are local. */
3004 if ((!info->extern_protected_data
3005 || (info->extern_protected_data < 0
3006 && !bed->extern_protected_data))
3007 && !bed->is_function_type (h->type))
3010 /* Function pointer equality tests may require that STV_PROTECTED
3011 symbols be treated as dynamic symbols. If the address of a
3012 function not defined in an executable is set to that function's
3013 plt entry in the executable, then the address of the function in
3014 a shared library must also be the plt entry in the executable. */
3015 return local_protected;
3018 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3019 aligned. Returns the first TLS output section. */
3021 struct bfd_section *
3022 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3024 struct bfd_section *sec, *tls;
3025 unsigned int align = 0;
3027 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3028 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3032 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3033 if (sec->alignment_power > align)
3034 align = sec->alignment_power;
3036 elf_hash_table (info)->tls_sec = tls;
3038 /* Ensure the alignment of the first section is the largest alignment,
3039 so that the tls segment starts aligned. */
3041 tls->alignment_power = align;
3046 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3048 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3049 Elf_Internal_Sym *sym)
3051 const struct elf_backend_data *bed;
3053 /* Local symbols do not count, but target specific ones might. */
3054 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3055 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3058 bed = get_elf_backend_data (abfd);
3059 /* Function symbols do not count. */
3060 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3063 /* If the section is undefined, then so is the symbol. */
3064 if (sym->st_shndx == SHN_UNDEF)
3067 /* If the symbol is defined in the common section, then
3068 it is a common definition and so does not count. */
3069 if (bed->common_definition (sym))
3072 /* If the symbol is in a target specific section then we
3073 must rely upon the backend to tell us what it is. */
3074 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3075 /* FIXME - this function is not coded yet:
3077 return _bfd_is_global_symbol_definition (abfd, sym);
3079 Instead for now assume that the definition is not global,
3080 Even if this is wrong, at least the linker will behave
3081 in the same way that it used to do. */
3087 /* Search the symbol table of the archive element of the archive ABFD
3088 whose archive map contains a mention of SYMDEF, and determine if
3089 the symbol is defined in this element. */
3091 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3093 Elf_Internal_Shdr * hdr;
3094 bfd_size_type symcount;
3095 bfd_size_type extsymcount;
3096 bfd_size_type extsymoff;
3097 Elf_Internal_Sym *isymbuf;
3098 Elf_Internal_Sym *isym;
3099 Elf_Internal_Sym *isymend;
3102 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3106 /* Return FALSE if the object has been claimed by plugin. */
3107 if (abfd->plugin_format == bfd_plugin_yes)
3110 if (! bfd_check_format (abfd, bfd_object))
3113 /* Select the appropriate symbol table. */
3114 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3115 hdr = &elf_tdata (abfd)->symtab_hdr;
3117 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3119 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3121 /* The sh_info field of the symtab header tells us where the
3122 external symbols start. We don't care about the local symbols. */
3123 if (elf_bad_symtab (abfd))
3125 extsymcount = symcount;
3130 extsymcount = symcount - hdr->sh_info;
3131 extsymoff = hdr->sh_info;
3134 if (extsymcount == 0)
3137 /* Read in the symbol table. */
3138 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3140 if (isymbuf == NULL)
3143 /* Scan the symbol table looking for SYMDEF. */
3145 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3149 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3154 if (strcmp (name, symdef->name) == 0)
3156 result = is_global_data_symbol_definition (abfd, isym);
3166 /* Add an entry to the .dynamic table. */
3169 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3173 struct elf_link_hash_table *hash_table;
3174 const struct elf_backend_data *bed;
3176 bfd_size_type newsize;
3177 bfd_byte *newcontents;
3178 Elf_Internal_Dyn dyn;
3180 hash_table = elf_hash_table (info);
3181 if (! is_elf_hash_table (hash_table))
3184 bed = get_elf_backend_data (hash_table->dynobj);
3185 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3186 BFD_ASSERT (s != NULL);
3188 newsize = s->size + bed->s->sizeof_dyn;
3189 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3190 if (newcontents == NULL)
3194 dyn.d_un.d_val = val;
3195 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3198 s->contents = newcontents;
3203 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3204 otherwise just check whether one already exists. Returns -1 on error,
3205 1 if a DT_NEEDED tag already exists, and 0 on success. */
3208 elf_add_dt_needed_tag (bfd *abfd,
3209 struct bfd_link_info *info,
3213 struct elf_link_hash_table *hash_table;
3214 bfd_size_type strindex;
3216 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3219 hash_table = elf_hash_table (info);
3220 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3221 if (strindex == (bfd_size_type) -1)
3224 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3227 const struct elf_backend_data *bed;
3230 bed = get_elf_backend_data (hash_table->dynobj);
3231 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3233 for (extdyn = sdyn->contents;
3234 extdyn < sdyn->contents + sdyn->size;
3235 extdyn += bed->s->sizeof_dyn)
3237 Elf_Internal_Dyn dyn;
3239 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3240 if (dyn.d_tag == DT_NEEDED
3241 && dyn.d_un.d_val == strindex)
3243 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3251 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3254 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3258 /* We were just checking for existence of the tag. */
3259 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3264 /* Return true if SONAME is on the needed list between NEEDED and STOP
3265 (or the end of list if STOP is NULL), and needed by a library that
3269 on_needed_list (const char *soname,
3270 struct bfd_link_needed_list *needed,
3271 struct bfd_link_needed_list *stop)
3273 struct bfd_link_needed_list *look;
3274 for (look = needed; look != stop; look = look->next)
3275 if (strcmp (soname, look->name) == 0
3276 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3277 /* If needed by a library that itself is not directly
3278 needed, recursively check whether that library is
3279 indirectly needed. Since we add DT_NEEDED entries to
3280 the end of the list, library dependencies appear after
3281 the library. Therefore search prior to the current
3282 LOOK, preventing possible infinite recursion. */
3283 || on_needed_list (elf_dt_name (look->by), needed, look)))
3289 /* Sort symbol by value, section, and size. */
3291 elf_sort_symbol (const void *arg1, const void *arg2)
3293 const struct elf_link_hash_entry *h1;
3294 const struct elf_link_hash_entry *h2;
3295 bfd_signed_vma vdiff;
3297 h1 = *(const struct elf_link_hash_entry **) arg1;
3298 h2 = *(const struct elf_link_hash_entry **) arg2;
3299 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3301 return vdiff > 0 ? 1 : -1;
3304 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3306 return sdiff > 0 ? 1 : -1;
3308 vdiff = h1->size - h2->size;
3309 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3312 /* This function is used to adjust offsets into .dynstr for
3313 dynamic symbols. This is called via elf_link_hash_traverse. */
3316 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3318 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3320 if (h->dynindx != -1)
3321 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3325 /* Assign string offsets in .dynstr, update all structures referencing
3329 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3331 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3332 struct elf_link_local_dynamic_entry *entry;
3333 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3334 bfd *dynobj = hash_table->dynobj;
3337 const struct elf_backend_data *bed;
3340 _bfd_elf_strtab_finalize (dynstr);
3341 size = _bfd_elf_strtab_size (dynstr);
3343 bed = get_elf_backend_data (dynobj);
3344 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3345 BFD_ASSERT (sdyn != NULL);
3347 /* Update all .dynamic entries referencing .dynstr strings. */
3348 for (extdyn = sdyn->contents;
3349 extdyn < sdyn->contents + sdyn->size;
3350 extdyn += bed->s->sizeof_dyn)
3352 Elf_Internal_Dyn dyn;
3354 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3358 dyn.d_un.d_val = size;
3368 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3373 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3376 /* Now update local dynamic symbols. */
3377 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3378 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3379 entry->isym.st_name);
3381 /* And the rest of dynamic symbols. */
3382 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3384 /* Adjust version definitions. */
3385 if (elf_tdata (output_bfd)->cverdefs)
3390 Elf_Internal_Verdef def;
3391 Elf_Internal_Verdaux defaux;
3393 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3397 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3399 p += sizeof (Elf_External_Verdef);
3400 if (def.vd_aux != sizeof (Elf_External_Verdef))
3402 for (i = 0; i < def.vd_cnt; ++i)
3404 _bfd_elf_swap_verdaux_in (output_bfd,
3405 (Elf_External_Verdaux *) p, &defaux);
3406 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3408 _bfd_elf_swap_verdaux_out (output_bfd,
3409 &defaux, (Elf_External_Verdaux *) p);
3410 p += sizeof (Elf_External_Verdaux);
3413 while (def.vd_next);
3416 /* Adjust version references. */
3417 if (elf_tdata (output_bfd)->verref)
3422 Elf_Internal_Verneed need;
3423 Elf_Internal_Vernaux needaux;
3425 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3429 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3431 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3432 _bfd_elf_swap_verneed_out (output_bfd, &need,
3433 (Elf_External_Verneed *) p);
3434 p += sizeof (Elf_External_Verneed);
3435 for (i = 0; i < need.vn_cnt; ++i)
3437 _bfd_elf_swap_vernaux_in (output_bfd,
3438 (Elf_External_Vernaux *) p, &needaux);
3439 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3441 _bfd_elf_swap_vernaux_out (output_bfd,
3443 (Elf_External_Vernaux *) p);
3444 p += sizeof (Elf_External_Vernaux);
3447 while (need.vn_next);
3453 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3454 The default is to only match when the INPUT and OUTPUT are exactly
3458 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3459 const bfd_target *output)
3461 return input == output;
3464 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3465 This version is used when different targets for the same architecture
3466 are virtually identical. */
3469 _bfd_elf_relocs_compatible (const bfd_target *input,
3470 const bfd_target *output)
3472 const struct elf_backend_data *obed, *ibed;
3474 if (input == output)
3477 ibed = xvec_get_elf_backend_data (input);
3478 obed = xvec_get_elf_backend_data (output);
3480 if (ibed->arch != obed->arch)
3483 /* If both backends are using this function, deem them compatible. */
3484 return ibed->relocs_compatible == obed->relocs_compatible;
3487 /* Make a special call to the linker "notice" function to tell it that
3488 we are about to handle an as-needed lib, or have finished
3489 processing the lib. */
3492 _bfd_elf_notice_as_needed (bfd *ibfd,
3493 struct bfd_link_info *info,
3494 enum notice_asneeded_action act)
3496 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3499 /* Check relocations an ELF object file. */
3502 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3504 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3505 struct elf_link_hash_table *htab = elf_hash_table (info);
3507 /* If this object is the same format as the output object, and it is
3508 not a shared library, then let the backend look through the
3511 This is required to build global offset table entries and to
3512 arrange for dynamic relocs. It is not required for the
3513 particular common case of linking non PIC code, even when linking
3514 against shared libraries, but unfortunately there is no way of
3515 knowing whether an object file has been compiled PIC or not.
3516 Looking through the relocs is not particularly time consuming.
3517 The problem is that we must either (1) keep the relocs in memory,
3518 which causes the linker to require additional runtime memory or
3519 (2) read the relocs twice from the input file, which wastes time.
3520 This would be a good case for using mmap.
3522 I have no idea how to handle linking PIC code into a file of a
3523 different format. It probably can't be done. */
3524 if ((abfd->flags & DYNAMIC) == 0
3525 && is_elf_hash_table (htab)
3526 && bed->check_relocs != NULL
3527 && elf_object_id (abfd) == elf_hash_table_id (htab)
3528 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3532 for (o = abfd->sections; o != NULL; o = o->next)
3534 Elf_Internal_Rela *internal_relocs;
3537 /* Don't check relocations in excluded sections. */
3538 if ((o->flags & SEC_RELOC) == 0
3539 || (o->flags & SEC_EXCLUDE) != 0
3540 || o->reloc_count == 0
3541 || ((info->strip == strip_all || info->strip == strip_debugger)
3542 && (o->flags & SEC_DEBUGGING) != 0)
3543 || bfd_is_abs_section (o->output_section))
3546 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3548 if (internal_relocs == NULL)
3551 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3553 if (elf_section_data (o)->relocs != internal_relocs)
3554 free (internal_relocs);
3564 /* Add symbols from an ELF object file to the linker hash table. */
3567 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3569 Elf_Internal_Ehdr *ehdr;
3570 Elf_Internal_Shdr *hdr;
3571 bfd_size_type symcount;
3572 bfd_size_type extsymcount;
3573 bfd_size_type extsymoff;
3574 struct elf_link_hash_entry **sym_hash;
3575 bfd_boolean dynamic;
3576 Elf_External_Versym *extversym = NULL;
3577 Elf_External_Versym *ever;
3578 struct elf_link_hash_entry *weaks;
3579 struct elf_link_hash_entry **nondeflt_vers = NULL;
3580 bfd_size_type nondeflt_vers_cnt = 0;
3581 Elf_Internal_Sym *isymbuf = NULL;
3582 Elf_Internal_Sym *isym;
3583 Elf_Internal_Sym *isymend;
3584 const struct elf_backend_data *bed;
3585 bfd_boolean add_needed;
3586 struct elf_link_hash_table *htab;
3588 void *alloc_mark = NULL;
3589 struct bfd_hash_entry **old_table = NULL;
3590 unsigned int old_size = 0;
3591 unsigned int old_count = 0;
3592 void *old_tab = NULL;
3594 struct bfd_link_hash_entry *old_undefs = NULL;
3595 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3596 long old_dynsymcount = 0;
3597 bfd_size_type old_dynstr_size = 0;
3600 bfd_boolean just_syms;
3602 htab = elf_hash_table (info);
3603 bed = get_elf_backend_data (abfd);
3605 if ((abfd->flags & DYNAMIC) == 0)
3611 /* You can't use -r against a dynamic object. Also, there's no
3612 hope of using a dynamic object which does not exactly match
3613 the format of the output file. */
3614 if (bfd_link_relocatable (info)
3615 || !is_elf_hash_table (htab)
3616 || info->output_bfd->xvec != abfd->xvec)
3618 if (bfd_link_relocatable (info))
3619 bfd_set_error (bfd_error_invalid_operation);
3621 bfd_set_error (bfd_error_wrong_format);
3626 ehdr = elf_elfheader (abfd);
3627 if (info->warn_alternate_em
3628 && bed->elf_machine_code != ehdr->e_machine
3629 && ((bed->elf_machine_alt1 != 0
3630 && ehdr->e_machine == bed->elf_machine_alt1)
3631 || (bed->elf_machine_alt2 != 0
3632 && ehdr->e_machine == bed->elf_machine_alt2)))
3633 info->callbacks->einfo
3634 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3635 ehdr->e_machine, abfd, bed->elf_machine_code);
3637 /* As a GNU extension, any input sections which are named
3638 .gnu.warning.SYMBOL are treated as warning symbols for the given
3639 symbol. This differs from .gnu.warning sections, which generate
3640 warnings when they are included in an output file. */
3641 /* PR 12761: Also generate this warning when building shared libraries. */
3642 for (s = abfd->sections; s != NULL; s = s->next)
3646 name = bfd_get_section_name (abfd, s);
3647 if (CONST_STRNEQ (name, ".gnu.warning."))
3652 name += sizeof ".gnu.warning." - 1;
3654 /* If this is a shared object, then look up the symbol
3655 in the hash table. If it is there, and it is already
3656 been defined, then we will not be using the entry
3657 from this shared object, so we don't need to warn.
3658 FIXME: If we see the definition in a regular object
3659 later on, we will warn, but we shouldn't. The only
3660 fix is to keep track of what warnings we are supposed
3661 to emit, and then handle them all at the end of the
3665 struct elf_link_hash_entry *h;
3667 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3669 /* FIXME: What about bfd_link_hash_common? */
3671 && (h->root.type == bfd_link_hash_defined
3672 || h->root.type == bfd_link_hash_defweak))
3677 msg = (char *) bfd_alloc (abfd, sz + 1);
3681 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3686 if (! (_bfd_generic_link_add_one_symbol
3687 (info, abfd, name, BSF_WARNING, s, 0, msg,
3688 FALSE, bed->collect, NULL)))
3691 if (bfd_link_executable (info))
3693 /* Clobber the section size so that the warning does
3694 not get copied into the output file. */
3697 /* Also set SEC_EXCLUDE, so that symbols defined in
3698 the warning section don't get copied to the output. */
3699 s->flags |= SEC_EXCLUDE;
3704 just_syms = ((s = abfd->sections) != NULL
3705 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3710 /* If we are creating a shared library, create all the dynamic
3711 sections immediately. We need to attach them to something,
3712 so we attach them to this BFD, provided it is the right
3713 format and is not from ld --just-symbols. Always create the
3714 dynamic sections for -E/--dynamic-list. FIXME: If there
3715 are no input BFD's of the same format as the output, we can't
3716 make a shared library. */
3718 && (bfd_link_pic (info)
3719 || (!bfd_link_relocatable (info)
3720 && (info->export_dynamic || info->dynamic)))
3721 && is_elf_hash_table (htab)
3722 && info->output_bfd->xvec == abfd->xvec
3723 && !htab->dynamic_sections_created)
3725 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3729 else if (!is_elf_hash_table (htab))
3733 const char *soname = NULL;
3735 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3738 /* ld --just-symbols and dynamic objects don't mix very well.
3739 ld shouldn't allow it. */
3743 /* If this dynamic lib was specified on the command line with
3744 --as-needed in effect, then we don't want to add a DT_NEEDED
3745 tag unless the lib is actually used. Similary for libs brought
3746 in by another lib's DT_NEEDED. When --no-add-needed is used
3747 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3748 any dynamic library in DT_NEEDED tags in the dynamic lib at
3750 add_needed = (elf_dyn_lib_class (abfd)
3751 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3752 | DYN_NO_NEEDED)) == 0;
3754 s = bfd_get_section_by_name (abfd, ".dynamic");
3759 unsigned int elfsec;
3760 unsigned long shlink;
3762 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3769 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3770 if (elfsec == SHN_BAD)
3771 goto error_free_dyn;
3772 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3774 for (extdyn = dynbuf;
3775 extdyn < dynbuf + s->size;
3776 extdyn += bed->s->sizeof_dyn)
3778 Elf_Internal_Dyn dyn;
3780 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3781 if (dyn.d_tag == DT_SONAME)
3783 unsigned int tagv = dyn.d_un.d_val;
3784 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3786 goto error_free_dyn;
3788 if (dyn.d_tag == DT_NEEDED)
3790 struct bfd_link_needed_list *n, **pn;
3792 unsigned int tagv = dyn.d_un.d_val;
3794 amt = sizeof (struct bfd_link_needed_list);
3795 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3796 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3797 if (n == NULL || fnm == NULL)
3798 goto error_free_dyn;
3799 amt = strlen (fnm) + 1;
3800 anm = (char *) bfd_alloc (abfd, amt);
3802 goto error_free_dyn;
3803 memcpy (anm, fnm, amt);
3807 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3811 if (dyn.d_tag == DT_RUNPATH)
3813 struct bfd_link_needed_list *n, **pn;
3815 unsigned int tagv = dyn.d_un.d_val;
3817 amt = sizeof (struct bfd_link_needed_list);
3818 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3819 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3820 if (n == NULL || fnm == NULL)
3821 goto error_free_dyn;
3822 amt = strlen (fnm) + 1;
3823 anm = (char *) bfd_alloc (abfd, amt);
3825 goto error_free_dyn;
3826 memcpy (anm, fnm, amt);
3830 for (pn = & runpath;
3836 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3837 if (!runpath && dyn.d_tag == DT_RPATH)
3839 struct bfd_link_needed_list *n, **pn;
3841 unsigned int tagv = dyn.d_un.d_val;
3843 amt = sizeof (struct bfd_link_needed_list);
3844 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3845 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3846 if (n == NULL || fnm == NULL)
3847 goto error_free_dyn;
3848 amt = strlen (fnm) + 1;
3849 anm = (char *) bfd_alloc (abfd, amt);
3851 goto error_free_dyn;
3852 memcpy (anm, fnm, amt);
3862 if (dyn.d_tag == DT_AUDIT)
3864 unsigned int tagv = dyn.d_un.d_val;
3865 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3872 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3873 frees all more recently bfd_alloc'd blocks as well. */
3879 struct bfd_link_needed_list **pn;
3880 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3885 /* We do not want to include any of the sections in a dynamic
3886 object in the output file. We hack by simply clobbering the
3887 list of sections in the BFD. This could be handled more
3888 cleanly by, say, a new section flag; the existing
3889 SEC_NEVER_LOAD flag is not the one we want, because that one
3890 still implies that the section takes up space in the output
3892 bfd_section_list_clear (abfd);
3894 /* Find the name to use in a DT_NEEDED entry that refers to this
3895 object. If the object has a DT_SONAME entry, we use it.
3896 Otherwise, if the generic linker stuck something in
3897 elf_dt_name, we use that. Otherwise, we just use the file
3899 if (soname == NULL || *soname == '\0')
3901 soname = elf_dt_name (abfd);
3902 if (soname == NULL || *soname == '\0')
3903 soname = bfd_get_filename (abfd);
3906 /* Save the SONAME because sometimes the linker emulation code
3907 will need to know it. */
3908 elf_dt_name (abfd) = soname;
3910 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3914 /* If we have already included this dynamic object in the
3915 link, just ignore it. There is no reason to include a
3916 particular dynamic object more than once. */
3920 /* Save the DT_AUDIT entry for the linker emulation code. */
3921 elf_dt_audit (abfd) = audit;
3924 /* If this is a dynamic object, we always link against the .dynsym
3925 symbol table, not the .symtab symbol table. The dynamic linker
3926 will only see the .dynsym symbol table, so there is no reason to
3927 look at .symtab for a dynamic object. */
3929 if (! dynamic || elf_dynsymtab (abfd) == 0)
3930 hdr = &elf_tdata (abfd)->symtab_hdr;
3932 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3934 symcount = hdr->sh_size / bed->s->sizeof_sym;
3936 /* The sh_info field of the symtab header tells us where the
3937 external symbols start. We don't care about the local symbols at
3939 if (elf_bad_symtab (abfd))
3941 extsymcount = symcount;
3946 extsymcount = symcount - hdr->sh_info;
3947 extsymoff = hdr->sh_info;
3950 sym_hash = elf_sym_hashes (abfd);
3951 if (extsymcount != 0)
3953 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3955 if (isymbuf == NULL)
3958 if (sym_hash == NULL)
3960 /* We store a pointer to the hash table entry for each
3962 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3963 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3964 if (sym_hash == NULL)
3965 goto error_free_sym;
3966 elf_sym_hashes (abfd) = sym_hash;
3972 /* Read in any version definitions. */
3973 if (!_bfd_elf_slurp_version_tables (abfd,
3974 info->default_imported_symver))
3975 goto error_free_sym;
3977 /* Read in the symbol versions, but don't bother to convert them
3978 to internal format. */
3979 if (elf_dynversym (abfd) != 0)
3981 Elf_Internal_Shdr *versymhdr;
3983 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3984 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3985 if (extversym == NULL)
3986 goto error_free_sym;
3987 amt = versymhdr->sh_size;
3988 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3989 || bfd_bread (extversym, amt, abfd) != amt)
3990 goto error_free_vers;
3994 /* If we are loading an as-needed shared lib, save the symbol table
3995 state before we start adding symbols. If the lib turns out
3996 to be unneeded, restore the state. */
3997 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4002 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4004 struct bfd_hash_entry *p;
4005 struct elf_link_hash_entry *h;
4007 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4009 h = (struct elf_link_hash_entry *) p;
4010 entsize += htab->root.table.entsize;
4011 if (h->root.type == bfd_link_hash_warning)
4012 entsize += htab->root.table.entsize;
4016 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4017 old_tab = bfd_malloc (tabsize + entsize);
4018 if (old_tab == NULL)
4019 goto error_free_vers;
4021 /* Remember the current objalloc pointer, so that all mem for
4022 symbols added can later be reclaimed. */
4023 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4024 if (alloc_mark == NULL)
4025 goto error_free_vers;
4027 /* Make a special call to the linker "notice" function to
4028 tell it that we are about to handle an as-needed lib. */
4029 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4030 goto error_free_vers;
4032 /* Clone the symbol table. Remember some pointers into the
4033 symbol table, and dynamic symbol count. */
4034 old_ent = (char *) old_tab + tabsize;
4035 memcpy (old_tab, htab->root.table.table, tabsize);
4036 old_undefs = htab->root.undefs;
4037 old_undefs_tail = htab->root.undefs_tail;
4038 old_table = htab->root.table.table;
4039 old_size = htab->root.table.size;
4040 old_count = htab->root.table.count;
4041 old_dynsymcount = htab->dynsymcount;
4042 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
4044 for (i = 0; i < htab->root.table.size; i++)
4046 struct bfd_hash_entry *p;
4047 struct elf_link_hash_entry *h;
4049 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4051 memcpy (old_ent, p, htab->root.table.entsize);
4052 old_ent = (char *) old_ent + htab->root.table.entsize;
4053 h = (struct elf_link_hash_entry *) p;
4054 if (h->root.type == bfd_link_hash_warning)
4056 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4057 old_ent = (char *) old_ent + htab->root.table.entsize;
4064 ever = extversym != NULL ? extversym + extsymoff : NULL;
4065 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4067 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4071 asection *sec, *new_sec;
4074 struct elf_link_hash_entry *h;
4075 struct elf_link_hash_entry *hi;
4076 bfd_boolean definition;
4077 bfd_boolean size_change_ok;
4078 bfd_boolean type_change_ok;
4079 bfd_boolean new_weakdef;
4080 bfd_boolean new_weak;
4081 bfd_boolean old_weak;
4082 bfd_boolean override;
4084 unsigned int old_alignment;
4086 bfd_boolean matched;
4090 flags = BSF_NO_FLAGS;
4092 value = isym->st_value;
4093 common = bed->common_definition (isym);
4095 bind = ELF_ST_BIND (isym->st_info);
4099 /* This should be impossible, since ELF requires that all
4100 global symbols follow all local symbols, and that sh_info
4101 point to the first global symbol. Unfortunately, Irix 5
4106 if (isym->st_shndx != SHN_UNDEF && !common)
4114 case STB_GNU_UNIQUE:
4115 flags = BSF_GNU_UNIQUE;
4119 /* Leave it up to the processor backend. */
4123 if (isym->st_shndx == SHN_UNDEF)
4124 sec = bfd_und_section_ptr;
4125 else if (isym->st_shndx == SHN_ABS)
4126 sec = bfd_abs_section_ptr;
4127 else if (isym->st_shndx == SHN_COMMON)
4129 sec = bfd_com_section_ptr;
4130 /* What ELF calls the size we call the value. What ELF
4131 calls the value we call the alignment. */
4132 value = isym->st_size;
4136 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4138 sec = bfd_abs_section_ptr;
4139 else if (discarded_section (sec))
4141 /* Symbols from discarded section are undefined. We keep
4143 sec = bfd_und_section_ptr;
4144 isym->st_shndx = SHN_UNDEF;
4146 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4150 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4153 goto error_free_vers;
4155 if (isym->st_shndx == SHN_COMMON
4156 && (abfd->flags & BFD_PLUGIN) != 0)
4158 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4162 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4164 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4166 goto error_free_vers;
4170 else if (isym->st_shndx == SHN_COMMON
4171 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4172 && !bfd_link_relocatable (info))
4174 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4178 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4179 | SEC_LINKER_CREATED);
4180 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4182 goto error_free_vers;
4186 else if (bed->elf_add_symbol_hook)
4188 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4190 goto error_free_vers;
4192 /* The hook function sets the name to NULL if this symbol
4193 should be skipped for some reason. */
4198 /* Sanity check that all possibilities were handled. */
4201 bfd_set_error (bfd_error_bad_value);
4202 goto error_free_vers;
4205 /* Silently discard TLS symbols from --just-syms. There's
4206 no way to combine a static TLS block with a new TLS block
4207 for this executable. */
4208 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4209 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4212 if (bfd_is_und_section (sec)
4213 || bfd_is_com_section (sec))
4218 size_change_ok = FALSE;
4219 type_change_ok = bed->type_change_ok;
4226 if (is_elf_hash_table (htab))
4228 Elf_Internal_Versym iver;
4229 unsigned int vernum = 0;
4234 if (info->default_imported_symver)
4235 /* Use the default symbol version created earlier. */
4236 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4241 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4243 vernum = iver.vs_vers & VERSYM_VERSION;
4245 /* If this is a hidden symbol, or if it is not version
4246 1, we append the version name to the symbol name.
4247 However, we do not modify a non-hidden absolute symbol
4248 if it is not a function, because it might be the version
4249 symbol itself. FIXME: What if it isn't? */
4250 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4252 && (!bfd_is_abs_section (sec)
4253 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4256 size_t namelen, verlen, newlen;
4259 if (isym->st_shndx != SHN_UNDEF)
4261 if (vernum > elf_tdata (abfd)->cverdefs)
4263 else if (vernum > 1)
4265 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4271 (*_bfd_error_handler)
4272 (_("%B: %s: invalid version %u (max %d)"),
4274 elf_tdata (abfd)->cverdefs);
4275 bfd_set_error (bfd_error_bad_value);
4276 goto error_free_vers;
4281 /* We cannot simply test for the number of
4282 entries in the VERNEED section since the
4283 numbers for the needed versions do not start
4285 Elf_Internal_Verneed *t;
4288 for (t = elf_tdata (abfd)->verref;
4292 Elf_Internal_Vernaux *a;
4294 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4296 if (a->vna_other == vernum)
4298 verstr = a->vna_nodename;
4307 (*_bfd_error_handler)
4308 (_("%B: %s: invalid needed version %d"),
4309 abfd, name, vernum);
4310 bfd_set_error (bfd_error_bad_value);
4311 goto error_free_vers;
4315 namelen = strlen (name);
4316 verlen = strlen (verstr);
4317 newlen = namelen + verlen + 2;
4318 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4319 && isym->st_shndx != SHN_UNDEF)
4322 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4323 if (newname == NULL)
4324 goto error_free_vers;
4325 memcpy (newname, name, namelen);
4326 p = newname + namelen;
4328 /* If this is a defined non-hidden version symbol,
4329 we add another @ to the name. This indicates the
4330 default version of the symbol. */
4331 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4332 && isym->st_shndx != SHN_UNDEF)
4334 memcpy (p, verstr, verlen + 1);
4339 /* If this symbol has default visibility and the user has
4340 requested we not re-export it, then mark it as hidden. */
4341 if (!bfd_is_und_section (sec)
4344 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4345 isym->st_other = (STV_HIDDEN
4346 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4348 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4349 sym_hash, &old_bfd, &old_weak,
4350 &old_alignment, &skip, &override,
4351 &type_change_ok, &size_change_ok,
4353 goto error_free_vers;
4358 /* Override a definition only if the new symbol matches the
4360 if (override && matched)
4364 while (h->root.type == bfd_link_hash_indirect
4365 || h->root.type == bfd_link_hash_warning)
4366 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4368 if (elf_tdata (abfd)->verdef != NULL
4371 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4374 if (! (_bfd_generic_link_add_one_symbol
4375 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4376 (struct bfd_link_hash_entry **) sym_hash)))
4377 goto error_free_vers;
4380 /* We need to make sure that indirect symbol dynamic flags are
4383 while (h->root.type == bfd_link_hash_indirect
4384 || h->root.type == bfd_link_hash_warning)
4385 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4389 new_weak = (flags & BSF_WEAK) != 0;
4390 new_weakdef = FALSE;
4394 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4395 && is_elf_hash_table (htab)
4396 && h->u.weakdef == NULL)
4398 /* Keep a list of all weak defined non function symbols from
4399 a dynamic object, using the weakdef field. Later in this
4400 function we will set the weakdef field to the correct
4401 value. We only put non-function symbols from dynamic
4402 objects on this list, because that happens to be the only
4403 time we need to know the normal symbol corresponding to a
4404 weak symbol, and the information is time consuming to
4405 figure out. If the weakdef field is not already NULL,
4406 then this symbol was already defined by some previous
4407 dynamic object, and we will be using that previous
4408 definition anyhow. */
4410 h->u.weakdef = weaks;
4415 /* Set the alignment of a common symbol. */
4416 if ((common || bfd_is_com_section (sec))
4417 && h->root.type == bfd_link_hash_common)
4422 align = bfd_log2 (isym->st_value);
4425 /* The new symbol is a common symbol in a shared object.
4426 We need to get the alignment from the section. */
4427 align = new_sec->alignment_power;
4429 if (align > old_alignment)
4430 h->root.u.c.p->alignment_power = align;
4432 h->root.u.c.p->alignment_power = old_alignment;
4435 if (is_elf_hash_table (htab))
4437 /* Set a flag in the hash table entry indicating the type of
4438 reference or definition we just found. A dynamic symbol
4439 is one which is referenced or defined by both a regular
4440 object and a shared object. */
4441 bfd_boolean dynsym = FALSE;
4443 /* Plugin symbols aren't normal. Don't set def_regular or
4444 ref_regular for them, or make them dynamic. */
4445 if ((abfd->flags & BFD_PLUGIN) != 0)
4452 if (bind != STB_WEAK)
4453 h->ref_regular_nonweak = 1;
4465 /* If the indirect symbol has been forced local, don't
4466 make the real symbol dynamic. */
4467 if ((h == hi || !hi->forced_local)
4468 && (bfd_link_dll (info)
4478 hi->ref_dynamic = 1;
4483 hi->def_dynamic = 1;
4486 /* If the indirect symbol has been forced local, don't
4487 make the real symbol dynamic. */
4488 if ((h == hi || !hi->forced_local)
4491 || (h->u.weakdef != NULL
4493 && h->u.weakdef->dynindx != -1)))
4497 /* Check to see if we need to add an indirect symbol for
4498 the default name. */
4500 || (!override && h->root.type == bfd_link_hash_common))
4501 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4502 sec, value, &old_bfd, &dynsym))
4503 goto error_free_vers;
4505 /* Check the alignment when a common symbol is involved. This
4506 can change when a common symbol is overridden by a normal
4507 definition or a common symbol is ignored due to the old
4508 normal definition. We need to make sure the maximum
4509 alignment is maintained. */
4510 if ((old_alignment || common)
4511 && h->root.type != bfd_link_hash_common)
4513 unsigned int common_align;
4514 unsigned int normal_align;
4515 unsigned int symbol_align;
4519 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4520 || h->root.type == bfd_link_hash_defweak);
4522 symbol_align = ffs (h->root.u.def.value) - 1;
4523 if (h->root.u.def.section->owner != NULL
4524 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4526 normal_align = h->root.u.def.section->alignment_power;
4527 if (normal_align > symbol_align)
4528 normal_align = symbol_align;
4531 normal_align = symbol_align;
4535 common_align = old_alignment;
4536 common_bfd = old_bfd;
4541 common_align = bfd_log2 (isym->st_value);
4543 normal_bfd = old_bfd;
4546 if (normal_align < common_align)
4548 /* PR binutils/2735 */
4549 if (normal_bfd == NULL)
4550 (*_bfd_error_handler)
4551 (_("Warning: alignment %u of common symbol `%s' in %B is"
4552 " greater than the alignment (%u) of its section %A"),
4553 common_bfd, h->root.u.def.section,
4554 1 << common_align, name, 1 << normal_align);
4556 (*_bfd_error_handler)
4557 (_("Warning: alignment %u of symbol `%s' in %B"
4558 " is smaller than %u in %B"),
4559 normal_bfd, common_bfd,
4560 1 << normal_align, name, 1 << common_align);
4564 /* Remember the symbol size if it isn't undefined. */
4565 if (isym->st_size != 0
4566 && isym->st_shndx != SHN_UNDEF
4567 && (definition || h->size == 0))
4570 && h->size != isym->st_size
4571 && ! size_change_ok)
4572 (*_bfd_error_handler)
4573 (_("Warning: size of symbol `%s' changed"
4574 " from %lu in %B to %lu in %B"),
4576 name, (unsigned long) h->size,
4577 (unsigned long) isym->st_size);
4579 h->size = isym->st_size;
4582 /* If this is a common symbol, then we always want H->SIZE
4583 to be the size of the common symbol. The code just above
4584 won't fix the size if a common symbol becomes larger. We
4585 don't warn about a size change here, because that is
4586 covered by --warn-common. Allow changes between different
4588 if (h->root.type == bfd_link_hash_common)
4589 h->size = h->root.u.c.size;
4591 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4592 && ((definition && !new_weak)
4593 || (old_weak && h->root.type == bfd_link_hash_common)
4594 || h->type == STT_NOTYPE))
4596 unsigned int type = ELF_ST_TYPE (isym->st_info);
4598 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4600 if (type == STT_GNU_IFUNC
4601 && (abfd->flags & DYNAMIC) != 0)
4604 if (h->type != type)
4606 if (h->type != STT_NOTYPE && ! type_change_ok)
4607 (*_bfd_error_handler)
4608 (_("Warning: type of symbol `%s' changed"
4609 " from %d to %d in %B"),
4610 abfd, name, h->type, type);
4616 /* Merge st_other field. */
4617 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4619 /* We don't want to make debug symbol dynamic. */
4621 && (sec->flags & SEC_DEBUGGING)
4622 && !bfd_link_relocatable (info))
4625 /* Nor should we make plugin symbols dynamic. */
4626 if ((abfd->flags & BFD_PLUGIN) != 0)
4631 h->target_internal = isym->st_target_internal;
4632 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4635 if (definition && !dynamic)
4637 char *p = strchr (name, ELF_VER_CHR);
4638 if (p != NULL && p[1] != ELF_VER_CHR)
4640 /* Queue non-default versions so that .symver x, x@FOO
4641 aliases can be checked. */
4644 amt = ((isymend - isym + 1)
4645 * sizeof (struct elf_link_hash_entry *));
4647 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4649 goto error_free_vers;
4651 nondeflt_vers[nondeflt_vers_cnt++] = h;
4655 if (dynsym && h->dynindx == -1)
4657 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4658 goto error_free_vers;
4659 if (h->u.weakdef != NULL
4661 && h->u.weakdef->dynindx == -1)
4663 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4664 goto error_free_vers;
4667 else if (h->dynindx != -1)
4668 /* If the symbol already has a dynamic index, but
4669 visibility says it should not be visible, turn it into
4671 switch (ELF_ST_VISIBILITY (h->other))
4675 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4680 /* Don't add DT_NEEDED for references from the dummy bfd nor
4681 for unmatched symbol. */
4686 && h->ref_regular_nonweak
4688 || (old_bfd->flags & BFD_PLUGIN) == 0))
4689 || (h->ref_dynamic_nonweak
4690 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4691 && !on_needed_list (elf_dt_name (abfd),
4692 htab->needed, NULL))))
4695 const char *soname = elf_dt_name (abfd);
4697 info->callbacks->minfo ("%!", soname, old_bfd,
4698 h->root.root.string);
4700 /* A symbol from a library loaded via DT_NEEDED of some
4701 other library is referenced by a regular object.
4702 Add a DT_NEEDED entry for it. Issue an error if
4703 --no-add-needed is used and the reference was not
4706 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4708 (*_bfd_error_handler)
4709 (_("%B: undefined reference to symbol '%s'"),
4711 bfd_set_error (bfd_error_missing_dso);
4712 goto error_free_vers;
4715 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4716 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4719 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4721 goto error_free_vers;
4723 BFD_ASSERT (ret == 0);
4728 if (extversym != NULL)
4734 if (isymbuf != NULL)
4740 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4744 /* Restore the symbol table. */
4745 old_ent = (char *) old_tab + tabsize;
4746 memset (elf_sym_hashes (abfd), 0,
4747 extsymcount * sizeof (struct elf_link_hash_entry *));
4748 htab->root.table.table = old_table;
4749 htab->root.table.size = old_size;
4750 htab->root.table.count = old_count;
4751 memcpy (htab->root.table.table, old_tab, tabsize);
4752 htab->root.undefs = old_undefs;
4753 htab->root.undefs_tail = old_undefs_tail;
4754 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4755 for (i = 0; i < htab->root.table.size; i++)
4757 struct bfd_hash_entry *p;
4758 struct elf_link_hash_entry *h;
4760 unsigned int alignment_power;
4762 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4764 h = (struct elf_link_hash_entry *) p;
4765 if (h->root.type == bfd_link_hash_warning)
4766 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4767 if (h->dynindx >= old_dynsymcount
4768 && h->dynstr_index < old_dynstr_size)
4769 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4771 /* Preserve the maximum alignment and size for common
4772 symbols even if this dynamic lib isn't on DT_NEEDED
4773 since it can still be loaded at run time by another
4775 if (h->root.type == bfd_link_hash_common)
4777 size = h->root.u.c.size;
4778 alignment_power = h->root.u.c.p->alignment_power;
4783 alignment_power = 0;
4785 memcpy (p, old_ent, htab->root.table.entsize);
4786 old_ent = (char *) old_ent + htab->root.table.entsize;
4787 h = (struct elf_link_hash_entry *) p;
4788 if (h->root.type == bfd_link_hash_warning)
4790 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4791 old_ent = (char *) old_ent + htab->root.table.entsize;
4792 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4794 if (h->root.type == bfd_link_hash_common)
4796 if (size > h->root.u.c.size)
4797 h->root.u.c.size = size;
4798 if (alignment_power > h->root.u.c.p->alignment_power)
4799 h->root.u.c.p->alignment_power = alignment_power;
4804 /* Make a special call to the linker "notice" function to
4805 tell it that symbols added for crefs may need to be removed. */
4806 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4807 goto error_free_vers;
4810 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4812 if (nondeflt_vers != NULL)
4813 free (nondeflt_vers);
4817 if (old_tab != NULL)
4819 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4820 goto error_free_vers;
4825 /* Now that all the symbols from this input file are created, if
4826 not performing a relocatable link, handle .symver foo, foo@BAR
4827 such that any relocs against foo become foo@BAR. */
4828 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4830 bfd_size_type cnt, symidx;
4832 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4834 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4835 char *shortname, *p;
4837 p = strchr (h->root.root.string, ELF_VER_CHR);
4839 || (h->root.type != bfd_link_hash_defined
4840 && h->root.type != bfd_link_hash_defweak))
4843 amt = p - h->root.root.string;
4844 shortname = (char *) bfd_malloc (amt + 1);
4846 goto error_free_vers;
4847 memcpy (shortname, h->root.root.string, amt);
4848 shortname[amt] = '\0';
4850 hi = (struct elf_link_hash_entry *)
4851 bfd_link_hash_lookup (&htab->root, shortname,
4852 FALSE, FALSE, FALSE);
4854 && hi->root.type == h->root.type
4855 && hi->root.u.def.value == h->root.u.def.value
4856 && hi->root.u.def.section == h->root.u.def.section)
4858 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4859 hi->root.type = bfd_link_hash_indirect;
4860 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4861 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4862 sym_hash = elf_sym_hashes (abfd);
4864 for (symidx = 0; symidx < extsymcount; ++symidx)
4865 if (sym_hash[symidx] == hi)
4867 sym_hash[symidx] = h;
4873 free (nondeflt_vers);
4874 nondeflt_vers = NULL;
4877 /* Now set the weakdefs field correctly for all the weak defined
4878 symbols we found. The only way to do this is to search all the
4879 symbols. Since we only need the information for non functions in
4880 dynamic objects, that's the only time we actually put anything on
4881 the list WEAKS. We need this information so that if a regular
4882 object refers to a symbol defined weakly in a dynamic object, the
4883 real symbol in the dynamic object is also put in the dynamic
4884 symbols; we also must arrange for both symbols to point to the
4885 same memory location. We could handle the general case of symbol
4886 aliasing, but a general symbol alias can only be generated in
4887 assembler code, handling it correctly would be very time
4888 consuming, and other ELF linkers don't handle general aliasing
4892 struct elf_link_hash_entry **hpp;
4893 struct elf_link_hash_entry **hppend;
4894 struct elf_link_hash_entry **sorted_sym_hash;
4895 struct elf_link_hash_entry *h;
4898 /* Since we have to search the whole symbol list for each weak
4899 defined symbol, search time for N weak defined symbols will be
4900 O(N^2). Binary search will cut it down to O(NlogN). */
4901 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4902 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4903 if (sorted_sym_hash == NULL)
4905 sym_hash = sorted_sym_hash;
4906 hpp = elf_sym_hashes (abfd);
4907 hppend = hpp + extsymcount;
4909 for (; hpp < hppend; hpp++)
4913 && h->root.type == bfd_link_hash_defined
4914 && !bed->is_function_type (h->type))
4922 qsort (sorted_sym_hash, sym_count,
4923 sizeof (struct elf_link_hash_entry *),
4926 while (weaks != NULL)
4928 struct elf_link_hash_entry *hlook;
4931 size_t i, j, idx = 0;
4934 weaks = hlook->u.weakdef;
4935 hlook->u.weakdef = NULL;
4937 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4938 || hlook->root.type == bfd_link_hash_defweak
4939 || hlook->root.type == bfd_link_hash_common
4940 || hlook->root.type == bfd_link_hash_indirect);
4941 slook = hlook->root.u.def.section;
4942 vlook = hlook->root.u.def.value;
4948 bfd_signed_vma vdiff;
4950 h = sorted_sym_hash[idx];
4951 vdiff = vlook - h->root.u.def.value;
4958 int sdiff = slook->id - h->root.u.def.section->id;
4968 /* We didn't find a value/section match. */
4972 /* With multiple aliases, or when the weak symbol is already
4973 strongly defined, we have multiple matching symbols and
4974 the binary search above may land on any of them. Step
4975 one past the matching symbol(s). */
4978 h = sorted_sym_hash[idx];
4979 if (h->root.u.def.section != slook
4980 || h->root.u.def.value != vlook)
4984 /* Now look back over the aliases. Since we sorted by size
4985 as well as value and section, we'll choose the one with
4986 the largest size. */
4989 h = sorted_sym_hash[idx];
4991 /* Stop if value or section doesn't match. */
4992 if (h->root.u.def.section != slook
4993 || h->root.u.def.value != vlook)
4995 else if (h != hlook)
4997 hlook->u.weakdef = h;
4999 /* If the weak definition is in the list of dynamic
5000 symbols, make sure the real definition is put
5002 if (hlook->dynindx != -1 && h->dynindx == -1)
5004 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5007 free (sorted_sym_hash);
5012 /* If the real definition is in the list of dynamic
5013 symbols, make sure the weak definition is put
5014 there as well. If we don't do this, then the
5015 dynamic loader might not merge the entries for the
5016 real definition and the weak definition. */
5017 if (h->dynindx != -1 && hlook->dynindx == -1)
5019 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5020 goto err_free_sym_hash;
5027 free (sorted_sym_hash);
5030 if (bed->check_directives
5031 && !(*bed->check_directives) (abfd, info))
5034 if (!info->check_relocs_after_open_input
5035 && !_bfd_elf_link_check_relocs (abfd, info))
5038 /* If this is a non-traditional link, try to optimize the handling
5039 of the .stab/.stabstr sections. */
5041 && ! info->traditional_format
5042 && is_elf_hash_table (htab)
5043 && (info->strip != strip_all && info->strip != strip_debugger))
5047 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5048 if (stabstr != NULL)
5050 bfd_size_type string_offset = 0;
5053 for (stab = abfd->sections; stab; stab = stab->next)
5054 if (CONST_STRNEQ (stab->name, ".stab")
5055 && (!stab->name[5] ||
5056 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5057 && (stab->flags & SEC_MERGE) == 0
5058 && !bfd_is_abs_section (stab->output_section))
5060 struct bfd_elf_section_data *secdata;
5062 secdata = elf_section_data (stab);
5063 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5064 stabstr, &secdata->sec_info,
5067 if (secdata->sec_info)
5068 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5073 if (is_elf_hash_table (htab) && add_needed)
5075 /* Add this bfd to the loaded list. */
5076 struct elf_link_loaded_list *n;
5078 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5082 n->next = htab->loaded;
5089 if (old_tab != NULL)
5091 if (nondeflt_vers != NULL)
5092 free (nondeflt_vers);
5093 if (extversym != NULL)
5096 if (isymbuf != NULL)
5102 /* Return the linker hash table entry of a symbol that might be
5103 satisfied by an archive symbol. Return -1 on error. */
5105 struct elf_link_hash_entry *
5106 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5107 struct bfd_link_info *info,
5110 struct elf_link_hash_entry *h;
5114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5118 /* If this is a default version (the name contains @@), look up the
5119 symbol again with only one `@' as well as without the version.
5120 The effect is that references to the symbol with and without the
5121 version will be matched by the default symbol in the archive. */
5123 p = strchr (name, ELF_VER_CHR);
5124 if (p == NULL || p[1] != ELF_VER_CHR)
5127 /* First check with only one `@'. */
5128 len = strlen (name);
5129 copy = (char *) bfd_alloc (abfd, len);
5131 return (struct elf_link_hash_entry *) 0 - 1;
5133 first = p - name + 1;
5134 memcpy (copy, name, first);
5135 memcpy (copy + first, name + first + 1, len - first);
5137 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5140 /* We also need to check references to the symbol without the
5142 copy[first - 1] = '\0';
5143 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5144 FALSE, FALSE, TRUE);
5147 bfd_release (abfd, copy);
5151 /* Add symbols from an ELF archive file to the linker hash table. We
5152 don't use _bfd_generic_link_add_archive_symbols because we need to
5153 handle versioned symbols.
5155 Fortunately, ELF archive handling is simpler than that done by
5156 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5157 oddities. In ELF, if we find a symbol in the archive map, and the
5158 symbol is currently undefined, we know that we must pull in that
5161 Unfortunately, we do have to make multiple passes over the symbol
5162 table until nothing further is resolved. */
5165 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5168 unsigned char *included = NULL;
5172 const struct elf_backend_data *bed;
5173 struct elf_link_hash_entry * (*archive_symbol_lookup)
5174 (bfd *, struct bfd_link_info *, const char *);
5176 if (! bfd_has_map (abfd))
5178 /* An empty archive is a special case. */
5179 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5181 bfd_set_error (bfd_error_no_armap);
5185 /* Keep track of all symbols we know to be already defined, and all
5186 files we know to be already included. This is to speed up the
5187 second and subsequent passes. */
5188 c = bfd_ardata (abfd)->symdef_count;
5192 amt *= sizeof (*included);
5193 included = (unsigned char *) bfd_zmalloc (amt);
5194 if (included == NULL)
5197 symdefs = bfd_ardata (abfd)->symdefs;
5198 bed = get_elf_backend_data (abfd);
5199 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5212 symdefend = symdef + c;
5213 for (i = 0; symdef < symdefend; symdef++, i++)
5215 struct elf_link_hash_entry *h;
5217 struct bfd_link_hash_entry *undefs_tail;
5222 if (symdef->file_offset == last)
5228 h = archive_symbol_lookup (abfd, info, symdef->name);
5229 if (h == (struct elf_link_hash_entry *) 0 - 1)
5235 if (h->root.type == bfd_link_hash_common)
5237 /* We currently have a common symbol. The archive map contains
5238 a reference to this symbol, so we may want to include it. We
5239 only want to include it however, if this archive element
5240 contains a definition of the symbol, not just another common
5243 Unfortunately some archivers (including GNU ar) will put
5244 declarations of common symbols into their archive maps, as
5245 well as real definitions, so we cannot just go by the archive
5246 map alone. Instead we must read in the element's symbol
5247 table and check that to see what kind of symbol definition
5249 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5252 else if (h->root.type != bfd_link_hash_undefined)
5254 if (h->root.type != bfd_link_hash_undefweak)
5255 /* Symbol must be defined. Don't check it again. */
5260 /* We need to include this archive member. */
5261 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5262 if (element == NULL)
5265 if (! bfd_check_format (element, bfd_object))
5268 undefs_tail = info->hash->undefs_tail;
5270 if (!(*info->callbacks
5271 ->add_archive_element) (info, element, symdef->name, &element))
5273 if (!bfd_link_add_symbols (element, info))
5276 /* If there are any new undefined symbols, we need to make
5277 another pass through the archive in order to see whether
5278 they can be defined. FIXME: This isn't perfect, because
5279 common symbols wind up on undefs_tail and because an
5280 undefined symbol which is defined later on in this pass
5281 does not require another pass. This isn't a bug, but it
5282 does make the code less efficient than it could be. */
5283 if (undefs_tail != info->hash->undefs_tail)
5286 /* Look backward to mark all symbols from this object file
5287 which we have already seen in this pass. */
5291 included[mark] = TRUE;
5296 while (symdefs[mark].file_offset == symdef->file_offset);
5298 /* We mark subsequent symbols from this object file as we go
5299 on through the loop. */
5300 last = symdef->file_offset;
5310 if (included != NULL)
5315 /* Given an ELF BFD, add symbols to the global hash table as
5319 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5321 switch (bfd_get_format (abfd))
5324 return elf_link_add_object_symbols (abfd, info);
5326 return elf_link_add_archive_symbols (abfd, info);
5328 bfd_set_error (bfd_error_wrong_format);
5333 struct hash_codes_info
5335 unsigned long *hashcodes;
5339 /* This function will be called though elf_link_hash_traverse to store
5340 all hash value of the exported symbols in an array. */
5343 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5345 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5350 /* Ignore indirect symbols. These are added by the versioning code. */
5351 if (h->dynindx == -1)
5354 name = h->root.root.string;
5355 if (h->versioned >= versioned)
5357 char *p = strchr (name, ELF_VER_CHR);
5360 alc = (char *) bfd_malloc (p - name + 1);
5366 memcpy (alc, name, p - name);
5367 alc[p - name] = '\0';
5372 /* Compute the hash value. */
5373 ha = bfd_elf_hash (name);
5375 /* Store the found hash value in the array given as the argument. */
5376 *(inf->hashcodes)++ = ha;
5378 /* And store it in the struct so that we can put it in the hash table
5380 h->u.elf_hash_value = ha;
5388 struct collect_gnu_hash_codes
5391 const struct elf_backend_data *bed;
5392 unsigned long int nsyms;
5393 unsigned long int maskbits;
5394 unsigned long int *hashcodes;
5395 unsigned long int *hashval;
5396 unsigned long int *indx;
5397 unsigned long int *counts;
5400 long int min_dynindx;
5401 unsigned long int bucketcount;
5402 unsigned long int symindx;
5403 long int local_indx;
5404 long int shift1, shift2;
5405 unsigned long int mask;
5409 /* This function will be called though elf_link_hash_traverse to store
5410 all hash value of the exported symbols in an array. */
5413 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5415 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5420 /* Ignore indirect symbols. These are added by the versioning code. */
5421 if (h->dynindx == -1)
5424 /* Ignore also local symbols and undefined symbols. */
5425 if (! (*s->bed->elf_hash_symbol) (h))
5428 name = h->root.root.string;
5429 if (h->versioned >= versioned)
5431 char *p = strchr (name, ELF_VER_CHR);
5434 alc = (char *) bfd_malloc (p - name + 1);
5440 memcpy (alc, name, p - name);
5441 alc[p - name] = '\0';
5446 /* Compute the hash value. */
5447 ha = bfd_elf_gnu_hash (name);
5449 /* Store the found hash value in the array for compute_bucket_count,
5450 and also for .dynsym reordering purposes. */
5451 s->hashcodes[s->nsyms] = ha;
5452 s->hashval[h->dynindx] = ha;
5454 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5455 s->min_dynindx = h->dynindx;
5463 /* This function will be called though elf_link_hash_traverse to do
5464 final dynaminc symbol renumbering. */
5467 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5469 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5470 unsigned long int bucket;
5471 unsigned long int val;
5473 /* Ignore indirect symbols. */
5474 if (h->dynindx == -1)
5477 /* Ignore also local symbols and undefined symbols. */
5478 if (! (*s->bed->elf_hash_symbol) (h))
5480 if (h->dynindx >= s->min_dynindx)
5481 h->dynindx = s->local_indx++;
5485 bucket = s->hashval[h->dynindx] % s->bucketcount;
5486 val = (s->hashval[h->dynindx] >> s->shift1)
5487 & ((s->maskbits >> s->shift1) - 1);
5488 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5490 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5491 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5492 if (s->counts[bucket] == 1)
5493 /* Last element terminates the chain. */
5495 bfd_put_32 (s->output_bfd, val,
5496 s->contents + (s->indx[bucket] - s->symindx) * 4);
5497 --s->counts[bucket];
5498 h->dynindx = s->indx[bucket]++;
5502 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5505 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5507 return !(h->forced_local
5508 || h->root.type == bfd_link_hash_undefined
5509 || h->root.type == bfd_link_hash_undefweak
5510 || ((h->root.type == bfd_link_hash_defined
5511 || h->root.type == bfd_link_hash_defweak)
5512 && h->root.u.def.section->output_section == NULL));
5515 /* Array used to determine the number of hash table buckets to use
5516 based on the number of symbols there are. If there are fewer than
5517 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5518 fewer than 37 we use 17 buckets, and so forth. We never use more
5519 than 32771 buckets. */
5521 static const size_t elf_buckets[] =
5523 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5527 /* Compute bucket count for hashing table. We do not use a static set
5528 of possible tables sizes anymore. Instead we determine for all
5529 possible reasonable sizes of the table the outcome (i.e., the
5530 number of collisions etc) and choose the best solution. The
5531 weighting functions are not too simple to allow the table to grow
5532 without bounds. Instead one of the weighting factors is the size.
5533 Therefore the result is always a good payoff between few collisions
5534 (= short chain lengths) and table size. */
5536 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5537 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5538 unsigned long int nsyms,
5541 size_t best_size = 0;
5542 unsigned long int i;
5544 /* We have a problem here. The following code to optimize the table
5545 size requires an integer type with more the 32 bits. If
5546 BFD_HOST_U_64_BIT is set we know about such a type. */
5547 #ifdef BFD_HOST_U_64_BIT
5552 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5553 bfd *dynobj = elf_hash_table (info)->dynobj;
5554 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5555 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5556 unsigned long int *counts;
5558 unsigned int no_improvement_count = 0;
5560 /* Possible optimization parameters: if we have NSYMS symbols we say
5561 that the hashing table must at least have NSYMS/4 and at most
5563 minsize = nsyms / 4;
5566 best_size = maxsize = nsyms * 2;
5571 if ((best_size & 31) == 0)
5575 /* Create array where we count the collisions in. We must use bfd_malloc
5576 since the size could be large. */
5578 amt *= sizeof (unsigned long int);
5579 counts = (unsigned long int *) bfd_malloc (amt);
5583 /* Compute the "optimal" size for the hash table. The criteria is a
5584 minimal chain length. The minor criteria is (of course) the size
5586 for (i = minsize; i < maxsize; ++i)
5588 /* Walk through the array of hashcodes and count the collisions. */
5589 BFD_HOST_U_64_BIT max;
5590 unsigned long int j;
5591 unsigned long int fact;
5593 if (gnu_hash && (i & 31) == 0)
5596 memset (counts, '\0', i * sizeof (unsigned long int));
5598 /* Determine how often each hash bucket is used. */
5599 for (j = 0; j < nsyms; ++j)
5600 ++counts[hashcodes[j] % i];
5602 /* For the weight function we need some information about the
5603 pagesize on the target. This is information need not be 100%
5604 accurate. Since this information is not available (so far) we
5605 define it here to a reasonable default value. If it is crucial
5606 to have a better value some day simply define this value. */
5607 # ifndef BFD_TARGET_PAGESIZE
5608 # define BFD_TARGET_PAGESIZE (4096)
5611 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5613 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5616 /* Variant 1: optimize for short chains. We add the squares
5617 of all the chain lengths (which favors many small chain
5618 over a few long chains). */
5619 for (j = 0; j < i; ++j)
5620 max += counts[j] * counts[j];
5622 /* This adds penalties for the overall size of the table. */
5623 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5626 /* Variant 2: Optimize a lot more for small table. Here we
5627 also add squares of the size but we also add penalties for
5628 empty slots (the +1 term). */
5629 for (j = 0; j < i; ++j)
5630 max += (1 + counts[j]) * (1 + counts[j]);
5632 /* The overall size of the table is considered, but not as
5633 strong as in variant 1, where it is squared. */
5634 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5638 /* Compare with current best results. */
5639 if (max < best_chlen)
5643 no_improvement_count = 0;
5645 /* PR 11843: Avoid futile long searches for the best bucket size
5646 when there are a large number of symbols. */
5647 else if (++no_improvement_count == 100)
5654 #endif /* defined (BFD_HOST_U_64_BIT) */
5656 /* This is the fallback solution if no 64bit type is available or if we
5657 are not supposed to spend much time on optimizations. We select the
5658 bucket count using a fixed set of numbers. */
5659 for (i = 0; elf_buckets[i] != 0; i++)
5661 best_size = elf_buckets[i];
5662 if (nsyms < elf_buckets[i + 1])
5665 if (gnu_hash && best_size < 2)
5672 /* Size any SHT_GROUP section for ld -r. */
5675 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5679 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5680 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5681 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5686 /* Set a default stack segment size. The value in INFO wins. If it
5687 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5688 undefined it is initialized. */
5691 bfd_elf_stack_segment_size (bfd *output_bfd,
5692 struct bfd_link_info *info,
5693 const char *legacy_symbol,
5694 bfd_vma default_size)
5696 struct elf_link_hash_entry *h = NULL;
5698 /* Look for legacy symbol. */
5700 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5701 FALSE, FALSE, FALSE);
5702 if (h && (h->root.type == bfd_link_hash_defined
5703 || h->root.type == bfd_link_hash_defweak)
5705 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5707 /* The symbol has no type if specified on the command line. */
5708 h->type = STT_OBJECT;
5709 if (info->stacksize)
5710 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5711 output_bfd, legacy_symbol);
5712 else if (h->root.u.def.section != bfd_abs_section_ptr)
5713 (*_bfd_error_handler) (_("%B: %s not absolute"),
5714 output_bfd, legacy_symbol);
5716 info->stacksize = h->root.u.def.value;
5719 if (!info->stacksize)
5720 /* If the user didn't set a size, or explicitly inhibit the
5721 size, set it now. */
5722 info->stacksize = default_size;
5724 /* Provide the legacy symbol, if it is referenced. */
5725 if (h && (h->root.type == bfd_link_hash_undefined
5726 || h->root.type == bfd_link_hash_undefweak))
5728 struct bfd_link_hash_entry *bh = NULL;
5730 if (!(_bfd_generic_link_add_one_symbol
5731 (info, output_bfd, legacy_symbol,
5732 BSF_GLOBAL, bfd_abs_section_ptr,
5733 info->stacksize >= 0 ? info->stacksize : 0,
5734 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5737 h = (struct elf_link_hash_entry *) bh;
5739 h->type = STT_OBJECT;
5745 /* Set up the sizes and contents of the ELF dynamic sections. This is
5746 called by the ELF linker emulation before_allocation routine. We
5747 must set the sizes of the sections before the linker sets the
5748 addresses of the various sections. */
5751 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5754 const char *filter_shlib,
5756 const char *depaudit,
5757 const char * const *auxiliary_filters,
5758 struct bfd_link_info *info,
5759 asection **sinterpptr)
5761 bfd_size_type soname_indx;
5763 const struct elf_backend_data *bed;
5764 struct elf_info_failed asvinfo;
5768 soname_indx = (bfd_size_type) -1;
5770 if (!is_elf_hash_table (info->hash))
5773 bed = get_elf_backend_data (output_bfd);
5775 /* Any syms created from now on start with -1 in
5776 got.refcount/offset and plt.refcount/offset. */
5777 elf_hash_table (info)->init_got_refcount
5778 = elf_hash_table (info)->init_got_offset;
5779 elf_hash_table (info)->init_plt_refcount
5780 = elf_hash_table (info)->init_plt_offset;
5782 if (bfd_link_relocatable (info)
5783 && !_bfd_elf_size_group_sections (info))
5786 /* The backend may have to create some sections regardless of whether
5787 we're dynamic or not. */
5788 if (bed->elf_backend_always_size_sections
5789 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5792 /* Determine any GNU_STACK segment requirements, after the backend
5793 has had a chance to set a default segment size. */
5794 if (info->execstack)
5795 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5796 else if (info->noexecstack)
5797 elf_stack_flags (output_bfd) = PF_R | PF_W;
5801 asection *notesec = NULL;
5804 for (inputobj = info->input_bfds;
5806 inputobj = inputobj->link.next)
5811 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5813 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5816 if (s->flags & SEC_CODE)
5820 else if (bed->default_execstack)
5823 if (notesec || info->stacksize > 0)
5824 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5825 if (notesec && exec && bfd_link_relocatable (info)
5826 && notesec->output_section != bfd_abs_section_ptr)
5827 notesec->output_section->flags |= SEC_CODE;
5830 dynobj = elf_hash_table (info)->dynobj;
5832 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5834 struct elf_info_failed eif;
5835 struct elf_link_hash_entry *h;
5837 struct bfd_elf_version_tree *t;
5838 struct bfd_elf_version_expr *d;
5840 bfd_boolean all_defined;
5842 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5843 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5847 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5849 if (soname_indx == (bfd_size_type) -1
5850 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5856 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5858 info->flags |= DF_SYMBOLIC;
5866 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5868 if (indx == (bfd_size_type) -1)
5871 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5872 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5876 if (filter_shlib != NULL)
5880 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5881 filter_shlib, TRUE);
5882 if (indx == (bfd_size_type) -1
5883 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5887 if (auxiliary_filters != NULL)
5889 const char * const *p;
5891 for (p = auxiliary_filters; *p != NULL; p++)
5895 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5897 if (indx == (bfd_size_type) -1
5898 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5907 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5909 if (indx == (bfd_size_type) -1
5910 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5914 if (depaudit != NULL)
5918 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5920 if (indx == (bfd_size_type) -1
5921 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5928 /* If we are supposed to export all symbols into the dynamic symbol
5929 table (this is not the normal case), then do so. */
5930 if (info->export_dynamic
5931 || (bfd_link_executable (info) && info->dynamic))
5933 elf_link_hash_traverse (elf_hash_table (info),
5934 _bfd_elf_export_symbol,
5940 /* Make all global versions with definition. */
5941 for (t = info->version_info; t != NULL; t = t->next)
5942 for (d = t->globals.list; d != NULL; d = d->next)
5943 if (!d->symver && d->literal)
5945 const char *verstr, *name;
5946 size_t namelen, verlen, newlen;
5947 char *newname, *p, leading_char;
5948 struct elf_link_hash_entry *newh;
5950 leading_char = bfd_get_symbol_leading_char (output_bfd);
5952 namelen = strlen (name) + (leading_char != '\0');
5954 verlen = strlen (verstr);
5955 newlen = namelen + verlen + 3;
5957 newname = (char *) bfd_malloc (newlen);
5958 if (newname == NULL)
5960 newname[0] = leading_char;
5961 memcpy (newname + (leading_char != '\0'), name, namelen);
5963 /* Check the hidden versioned definition. */
5964 p = newname + namelen;
5966 memcpy (p, verstr, verlen + 1);
5967 newh = elf_link_hash_lookup (elf_hash_table (info),
5968 newname, FALSE, FALSE,
5971 || (newh->root.type != bfd_link_hash_defined
5972 && newh->root.type != bfd_link_hash_defweak))
5974 /* Check the default versioned definition. */
5976 memcpy (p, verstr, verlen + 1);
5977 newh = elf_link_hash_lookup (elf_hash_table (info),
5978 newname, FALSE, FALSE,
5983 /* Mark this version if there is a definition and it is
5984 not defined in a shared object. */
5986 && !newh->def_dynamic
5987 && (newh->root.type == bfd_link_hash_defined
5988 || newh->root.type == bfd_link_hash_defweak))
5992 /* Attach all the symbols to their version information. */
5993 asvinfo.info = info;
5994 asvinfo.failed = FALSE;
5996 elf_link_hash_traverse (elf_hash_table (info),
5997 _bfd_elf_link_assign_sym_version,
6002 if (!info->allow_undefined_version)
6004 /* Check if all global versions have a definition. */
6006 for (t = info->version_info; t != NULL; t = t->next)
6007 for (d = t->globals.list; d != NULL; d = d->next)
6008 if (d->literal && !d->symver && !d->script)
6010 (*_bfd_error_handler)
6011 (_("%s: undefined version: %s"),
6012 d->pattern, t->name);
6013 all_defined = FALSE;
6018 bfd_set_error (bfd_error_bad_value);
6023 /* Find all symbols which were defined in a dynamic object and make
6024 the backend pick a reasonable value for them. */
6025 elf_link_hash_traverse (elf_hash_table (info),
6026 _bfd_elf_adjust_dynamic_symbol,
6031 /* Add some entries to the .dynamic section. We fill in some of the
6032 values later, in bfd_elf_final_link, but we must add the entries
6033 now so that we know the final size of the .dynamic section. */
6035 /* If there are initialization and/or finalization functions to
6036 call then add the corresponding DT_INIT/DT_FINI entries. */
6037 h = (info->init_function
6038 ? elf_link_hash_lookup (elf_hash_table (info),
6039 info->init_function, FALSE,
6046 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6049 h = (info->fini_function
6050 ? elf_link_hash_lookup (elf_hash_table (info),
6051 info->fini_function, FALSE,
6058 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6062 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6063 if (s != NULL && s->linker_has_input)
6065 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6066 if (! bfd_link_executable (info))
6071 for (sub = info->input_bfds; sub != NULL;
6072 sub = sub->link.next)
6073 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6074 for (o = sub->sections; o != NULL; o = o->next)
6075 if (elf_section_data (o)->this_hdr.sh_type
6076 == SHT_PREINIT_ARRAY)
6078 (*_bfd_error_handler)
6079 (_("%B: .preinit_array section is not allowed in DSO"),
6084 bfd_set_error (bfd_error_nonrepresentable_section);
6088 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6089 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6092 s = bfd_get_section_by_name (output_bfd, ".init_array");
6093 if (s != NULL && s->linker_has_input)
6095 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6096 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6099 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6100 if (s != NULL && s->linker_has_input)
6102 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6103 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6107 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6108 /* If .dynstr is excluded from the link, we don't want any of
6109 these tags. Strictly, we should be checking each section
6110 individually; This quick check covers for the case where
6111 someone does a /DISCARD/ : { *(*) }. */
6112 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6114 bfd_size_type strsize;
6116 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6117 if ((info->emit_hash
6118 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6119 || (info->emit_gnu_hash
6120 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6121 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6122 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6123 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6124 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6125 bed->s->sizeof_sym))
6130 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6133 /* The backend must work out the sizes of all the other dynamic
6136 && bed->elf_backend_size_dynamic_sections != NULL
6137 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6140 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6142 unsigned long section_sym_count;
6143 struct bfd_elf_version_tree *verdefs;
6146 /* Set up the version definition section. */
6147 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6148 BFD_ASSERT (s != NULL);
6150 /* We may have created additional version definitions if we are
6151 just linking a regular application. */
6152 verdefs = info->version_info;
6154 /* Skip anonymous version tag. */
6155 if (verdefs != NULL && verdefs->vernum == 0)
6156 verdefs = verdefs->next;
6158 if (verdefs == NULL && !info->create_default_symver)
6159 s->flags |= SEC_EXCLUDE;
6164 struct bfd_elf_version_tree *t;
6166 Elf_Internal_Verdef def;
6167 Elf_Internal_Verdaux defaux;
6168 struct bfd_link_hash_entry *bh;
6169 struct elf_link_hash_entry *h;
6175 /* Make space for the base version. */
6176 size += sizeof (Elf_External_Verdef);
6177 size += sizeof (Elf_External_Verdaux);
6180 /* Make space for the default version. */
6181 if (info->create_default_symver)
6183 size += sizeof (Elf_External_Verdef);
6187 for (t = verdefs; t != NULL; t = t->next)
6189 struct bfd_elf_version_deps *n;
6191 /* Don't emit base version twice. */
6195 size += sizeof (Elf_External_Verdef);
6196 size += sizeof (Elf_External_Verdaux);
6199 for (n = t->deps; n != NULL; n = n->next)
6200 size += sizeof (Elf_External_Verdaux);
6204 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6205 if (s->contents == NULL && s->size != 0)
6208 /* Fill in the version definition section. */
6212 def.vd_version = VER_DEF_CURRENT;
6213 def.vd_flags = VER_FLG_BASE;
6216 if (info->create_default_symver)
6218 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6219 def.vd_next = sizeof (Elf_External_Verdef);
6223 def.vd_aux = sizeof (Elf_External_Verdef);
6224 def.vd_next = (sizeof (Elf_External_Verdef)
6225 + sizeof (Elf_External_Verdaux));
6228 if (soname_indx != (bfd_size_type) -1)
6230 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6232 def.vd_hash = bfd_elf_hash (soname);
6233 defaux.vda_name = soname_indx;
6240 name = lbasename (output_bfd->filename);
6241 def.vd_hash = bfd_elf_hash (name);
6242 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6244 if (indx == (bfd_size_type) -1)
6246 defaux.vda_name = indx;
6248 defaux.vda_next = 0;
6250 _bfd_elf_swap_verdef_out (output_bfd, &def,
6251 (Elf_External_Verdef *) p);
6252 p += sizeof (Elf_External_Verdef);
6253 if (info->create_default_symver)
6255 /* Add a symbol representing this version. */
6257 if (! (_bfd_generic_link_add_one_symbol
6258 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6260 get_elf_backend_data (dynobj)->collect, &bh)))
6262 h = (struct elf_link_hash_entry *) bh;
6265 h->type = STT_OBJECT;
6266 h->verinfo.vertree = NULL;
6268 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6271 /* Create a duplicate of the base version with the same
6272 aux block, but different flags. */
6275 def.vd_aux = sizeof (Elf_External_Verdef);
6277 def.vd_next = (sizeof (Elf_External_Verdef)
6278 + sizeof (Elf_External_Verdaux));
6281 _bfd_elf_swap_verdef_out (output_bfd, &def,
6282 (Elf_External_Verdef *) p);
6283 p += sizeof (Elf_External_Verdef);
6285 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6286 (Elf_External_Verdaux *) p);
6287 p += sizeof (Elf_External_Verdaux);
6289 for (t = verdefs; t != NULL; t = t->next)
6292 struct bfd_elf_version_deps *n;
6294 /* Don't emit the base version twice. */
6299 for (n = t->deps; n != NULL; n = n->next)
6302 /* Add a symbol representing this version. */
6304 if (! (_bfd_generic_link_add_one_symbol
6305 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6307 get_elf_backend_data (dynobj)->collect, &bh)))
6309 h = (struct elf_link_hash_entry *) bh;
6312 h->type = STT_OBJECT;
6313 h->verinfo.vertree = t;
6315 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6318 def.vd_version = VER_DEF_CURRENT;
6320 if (t->globals.list == NULL
6321 && t->locals.list == NULL
6323 def.vd_flags |= VER_FLG_WEAK;
6324 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6325 def.vd_cnt = cdeps + 1;
6326 def.vd_hash = bfd_elf_hash (t->name);
6327 def.vd_aux = sizeof (Elf_External_Verdef);
6330 /* If a basever node is next, it *must* be the last node in
6331 the chain, otherwise Verdef construction breaks. */
6332 if (t->next != NULL && t->next->vernum == 0)
6333 BFD_ASSERT (t->next->next == NULL);
6335 if (t->next != NULL && t->next->vernum != 0)
6336 def.vd_next = (sizeof (Elf_External_Verdef)
6337 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6339 _bfd_elf_swap_verdef_out (output_bfd, &def,
6340 (Elf_External_Verdef *) p);
6341 p += sizeof (Elf_External_Verdef);
6343 defaux.vda_name = h->dynstr_index;
6344 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6346 defaux.vda_next = 0;
6347 if (t->deps != NULL)
6348 defaux.vda_next = sizeof (Elf_External_Verdaux);
6349 t->name_indx = defaux.vda_name;
6351 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6352 (Elf_External_Verdaux *) p);
6353 p += sizeof (Elf_External_Verdaux);
6355 for (n = t->deps; n != NULL; n = n->next)
6357 if (n->version_needed == NULL)
6359 /* This can happen if there was an error in the
6361 defaux.vda_name = 0;
6365 defaux.vda_name = n->version_needed->name_indx;
6366 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6369 if (n->next == NULL)
6370 defaux.vda_next = 0;
6372 defaux.vda_next = sizeof (Elf_External_Verdaux);
6374 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6375 (Elf_External_Verdaux *) p);
6376 p += sizeof (Elf_External_Verdaux);
6380 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6381 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6384 elf_tdata (output_bfd)->cverdefs = cdefs;
6387 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6389 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6392 else if (info->flags & DF_BIND_NOW)
6394 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6400 if (bfd_link_executable (info))
6401 info->flags_1 &= ~ (DF_1_INITFIRST
6404 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6408 /* Work out the size of the version reference section. */
6410 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6411 BFD_ASSERT (s != NULL);
6413 struct elf_find_verdep_info sinfo;
6416 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6417 if (sinfo.vers == 0)
6419 sinfo.failed = FALSE;
6421 elf_link_hash_traverse (elf_hash_table (info),
6422 _bfd_elf_link_find_version_dependencies,
6427 if (elf_tdata (output_bfd)->verref == NULL)
6428 s->flags |= SEC_EXCLUDE;
6431 Elf_Internal_Verneed *t;
6436 /* Build the version dependency section. */
6439 for (t = elf_tdata (output_bfd)->verref;
6443 Elf_Internal_Vernaux *a;
6445 size += sizeof (Elf_External_Verneed);
6447 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6448 size += sizeof (Elf_External_Vernaux);
6452 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6453 if (s->contents == NULL)
6457 for (t = elf_tdata (output_bfd)->verref;
6462 Elf_Internal_Vernaux *a;
6466 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6469 t->vn_version = VER_NEED_CURRENT;
6471 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6472 elf_dt_name (t->vn_bfd) != NULL
6473 ? elf_dt_name (t->vn_bfd)
6474 : lbasename (t->vn_bfd->filename),
6476 if (indx == (bfd_size_type) -1)
6479 t->vn_aux = sizeof (Elf_External_Verneed);
6480 if (t->vn_nextref == NULL)
6483 t->vn_next = (sizeof (Elf_External_Verneed)
6484 + caux * sizeof (Elf_External_Vernaux));
6486 _bfd_elf_swap_verneed_out (output_bfd, t,
6487 (Elf_External_Verneed *) p);
6488 p += sizeof (Elf_External_Verneed);
6490 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6492 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6493 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6494 a->vna_nodename, FALSE);
6495 if (indx == (bfd_size_type) -1)
6498 if (a->vna_nextptr == NULL)
6501 a->vna_next = sizeof (Elf_External_Vernaux);
6503 _bfd_elf_swap_vernaux_out (output_bfd, a,
6504 (Elf_External_Vernaux *) p);
6505 p += sizeof (Elf_External_Vernaux);
6509 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6510 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6513 elf_tdata (output_bfd)->cverrefs = crefs;
6517 if ((elf_tdata (output_bfd)->cverrefs == 0
6518 && elf_tdata (output_bfd)->cverdefs == 0)
6519 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6520 §ion_sym_count) == 0)
6522 s = bfd_get_linker_section (dynobj, ".gnu.version");
6523 s->flags |= SEC_EXCLUDE;
6529 /* Find the first non-excluded output section. We'll use its
6530 section symbol for some emitted relocs. */
6532 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6536 for (s = output_bfd->sections; s != NULL; s = s->next)
6537 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6538 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6540 elf_hash_table (info)->text_index_section = s;
6545 /* Find two non-excluded output sections, one for code, one for data.
6546 We'll use their section symbols for some emitted relocs. */
6548 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6552 /* Data first, since setting text_index_section changes
6553 _bfd_elf_link_omit_section_dynsym. */
6554 for (s = output_bfd->sections; s != NULL; s = s->next)
6555 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6556 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6558 elf_hash_table (info)->data_index_section = s;
6562 for (s = output_bfd->sections; s != NULL; s = s->next)
6563 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6564 == (SEC_ALLOC | SEC_READONLY))
6565 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6567 elf_hash_table (info)->text_index_section = s;
6571 if (elf_hash_table (info)->text_index_section == NULL)
6572 elf_hash_table (info)->text_index_section
6573 = elf_hash_table (info)->data_index_section;
6577 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6579 const struct elf_backend_data *bed;
6581 if (!is_elf_hash_table (info->hash))
6584 bed = get_elf_backend_data (output_bfd);
6585 (*bed->elf_backend_init_index_section) (output_bfd, info);
6587 if (elf_hash_table (info)->dynamic_sections_created)
6591 bfd_size_type dynsymcount;
6592 unsigned long section_sym_count;
6593 unsigned int dtagcount;
6595 dynobj = elf_hash_table (info)->dynobj;
6597 /* Assign dynsym indicies. In a shared library we generate a
6598 section symbol for each output section, which come first.
6599 Next come all of the back-end allocated local dynamic syms,
6600 followed by the rest of the global symbols. */
6602 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6603 §ion_sym_count);
6605 /* Work out the size of the symbol version section. */
6606 s = bfd_get_linker_section (dynobj, ".gnu.version");
6607 BFD_ASSERT (s != NULL);
6608 if (dynsymcount != 0
6609 && (s->flags & SEC_EXCLUDE) == 0)
6611 s->size = dynsymcount * sizeof (Elf_External_Versym);
6612 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6613 if (s->contents == NULL)
6616 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6620 /* Set the size of the .dynsym and .hash sections. We counted
6621 the number of dynamic symbols in elf_link_add_object_symbols.
6622 We will build the contents of .dynsym and .hash when we build
6623 the final symbol table, because until then we do not know the
6624 correct value to give the symbols. We built the .dynstr
6625 section as we went along in elf_link_add_object_symbols. */
6626 s = elf_hash_table (info)->dynsym;
6627 BFD_ASSERT (s != NULL);
6628 s->size = dynsymcount * bed->s->sizeof_sym;
6630 if (dynsymcount != 0)
6632 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6633 if (s->contents == NULL)
6636 /* The first entry in .dynsym is a dummy symbol.
6637 Clear all the section syms, in case we don't output them all. */
6638 ++section_sym_count;
6639 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6642 elf_hash_table (info)->bucketcount = 0;
6644 /* Compute the size of the hashing table. As a side effect this
6645 computes the hash values for all the names we export. */
6646 if (info->emit_hash)
6648 unsigned long int *hashcodes;
6649 struct hash_codes_info hashinf;
6651 unsigned long int nsyms;
6653 size_t hash_entry_size;
6655 /* Compute the hash values for all exported symbols. At the same
6656 time store the values in an array so that we could use them for
6658 amt = dynsymcount * sizeof (unsigned long int);
6659 hashcodes = (unsigned long int *) bfd_malloc (amt);
6660 if (hashcodes == NULL)
6662 hashinf.hashcodes = hashcodes;
6663 hashinf.error = FALSE;
6665 /* Put all hash values in HASHCODES. */
6666 elf_link_hash_traverse (elf_hash_table (info),
6667 elf_collect_hash_codes, &hashinf);
6674 nsyms = hashinf.hashcodes - hashcodes;
6676 = compute_bucket_count (info, hashcodes, nsyms, 0);
6679 if (bucketcount == 0)
6682 elf_hash_table (info)->bucketcount = bucketcount;
6684 s = bfd_get_linker_section (dynobj, ".hash");
6685 BFD_ASSERT (s != NULL);
6686 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6687 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6688 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6689 if (s->contents == NULL)
6692 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6693 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6694 s->contents + hash_entry_size);
6697 if (info->emit_gnu_hash)
6700 unsigned char *contents;
6701 struct collect_gnu_hash_codes cinfo;
6705 memset (&cinfo, 0, sizeof (cinfo));
6707 /* Compute the hash values for all exported symbols. At the same
6708 time store the values in an array so that we could use them for
6710 amt = dynsymcount * 2 * sizeof (unsigned long int);
6711 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6712 if (cinfo.hashcodes == NULL)
6715 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6716 cinfo.min_dynindx = -1;
6717 cinfo.output_bfd = output_bfd;
6720 /* Put all hash values in HASHCODES. */
6721 elf_link_hash_traverse (elf_hash_table (info),
6722 elf_collect_gnu_hash_codes, &cinfo);
6725 free (cinfo.hashcodes);
6730 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6732 if (bucketcount == 0)
6734 free (cinfo.hashcodes);
6738 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6739 BFD_ASSERT (s != NULL);
6741 if (cinfo.nsyms == 0)
6743 /* Empty .gnu.hash section is special. */
6744 BFD_ASSERT (cinfo.min_dynindx == -1);
6745 free (cinfo.hashcodes);
6746 s->size = 5 * 4 + bed->s->arch_size / 8;
6747 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6748 if (contents == NULL)
6750 s->contents = contents;
6751 /* 1 empty bucket. */
6752 bfd_put_32 (output_bfd, 1, contents);
6753 /* SYMIDX above the special symbol 0. */
6754 bfd_put_32 (output_bfd, 1, contents + 4);
6755 /* Just one word for bitmask. */
6756 bfd_put_32 (output_bfd, 1, contents + 8);
6757 /* Only hash fn bloom filter. */
6758 bfd_put_32 (output_bfd, 0, contents + 12);
6759 /* No hashes are valid - empty bitmask. */
6760 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6761 /* No hashes in the only bucket. */
6762 bfd_put_32 (output_bfd, 0,
6763 contents + 16 + bed->s->arch_size / 8);
6767 unsigned long int maskwords, maskbitslog2, x;
6768 BFD_ASSERT (cinfo.min_dynindx != -1);
6772 while ((x >>= 1) != 0)
6774 if (maskbitslog2 < 3)
6776 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6777 maskbitslog2 = maskbitslog2 + 3;
6779 maskbitslog2 = maskbitslog2 + 2;
6780 if (bed->s->arch_size == 64)
6782 if (maskbitslog2 == 5)
6788 cinfo.mask = (1 << cinfo.shift1) - 1;
6789 cinfo.shift2 = maskbitslog2;
6790 cinfo.maskbits = 1 << maskbitslog2;
6791 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6792 amt = bucketcount * sizeof (unsigned long int) * 2;
6793 amt += maskwords * sizeof (bfd_vma);
6794 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6795 if (cinfo.bitmask == NULL)
6797 free (cinfo.hashcodes);
6801 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6802 cinfo.indx = cinfo.counts + bucketcount;
6803 cinfo.symindx = dynsymcount - cinfo.nsyms;
6804 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6806 /* Determine how often each hash bucket is used. */
6807 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6808 for (i = 0; i < cinfo.nsyms; ++i)
6809 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6811 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6812 if (cinfo.counts[i] != 0)
6814 cinfo.indx[i] = cnt;
6815 cnt += cinfo.counts[i];
6817 BFD_ASSERT (cnt == dynsymcount);
6818 cinfo.bucketcount = bucketcount;
6819 cinfo.local_indx = cinfo.min_dynindx;
6821 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6822 s->size += cinfo.maskbits / 8;
6823 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6824 if (contents == NULL)
6826 free (cinfo.bitmask);
6827 free (cinfo.hashcodes);
6831 s->contents = contents;
6832 bfd_put_32 (output_bfd, bucketcount, contents);
6833 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6834 bfd_put_32 (output_bfd, maskwords, contents + 8);
6835 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6836 contents += 16 + cinfo.maskbits / 8;
6838 for (i = 0; i < bucketcount; ++i)
6840 if (cinfo.counts[i] == 0)
6841 bfd_put_32 (output_bfd, 0, contents);
6843 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6847 cinfo.contents = contents;
6849 /* Renumber dynamic symbols, populate .gnu.hash section. */
6850 elf_link_hash_traverse (elf_hash_table (info),
6851 elf_renumber_gnu_hash_syms, &cinfo);
6853 contents = s->contents + 16;
6854 for (i = 0; i < maskwords; ++i)
6856 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6858 contents += bed->s->arch_size / 8;
6861 free (cinfo.bitmask);
6862 free (cinfo.hashcodes);
6866 s = bfd_get_linker_section (dynobj, ".dynstr");
6867 BFD_ASSERT (s != NULL);
6869 elf_finalize_dynstr (output_bfd, info);
6871 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6873 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6874 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6881 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6884 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6887 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6888 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6891 /* Finish SHF_MERGE section merging. */
6894 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6899 if (!is_elf_hash_table (info->hash))
6902 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6903 if ((ibfd->flags & DYNAMIC) == 0
6904 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6905 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6906 == get_elf_backend_data (obfd)->s->elfclass))
6907 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6908 if ((sec->flags & SEC_MERGE) != 0
6909 && !bfd_is_abs_section (sec->output_section))
6911 struct bfd_elf_section_data *secdata;
6913 secdata = elf_section_data (sec);
6914 if (! _bfd_add_merge_section (obfd,
6915 &elf_hash_table (info)->merge_info,
6916 sec, &secdata->sec_info))
6918 else if (secdata->sec_info)
6919 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6922 if (elf_hash_table (info)->merge_info != NULL)
6923 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6924 merge_sections_remove_hook);
6928 /* Create an entry in an ELF linker hash table. */
6930 struct bfd_hash_entry *
6931 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6932 struct bfd_hash_table *table,
6935 /* Allocate the structure if it has not already been allocated by a
6939 entry = (struct bfd_hash_entry *)
6940 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6945 /* Call the allocation method of the superclass. */
6946 entry = _bfd_link_hash_newfunc (entry, table, string);
6949 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6950 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6952 /* Set local fields. */
6955 ret->got = htab->init_got_refcount;
6956 ret->plt = htab->init_plt_refcount;
6957 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6958 - offsetof (struct elf_link_hash_entry, size)));
6959 /* Assume that we have been called by a non-ELF symbol reader.
6960 This flag is then reset by the code which reads an ELF input
6961 file. This ensures that a symbol created by a non-ELF symbol
6962 reader will have the flag set correctly. */
6969 /* Copy data from an indirect symbol to its direct symbol, hiding the
6970 old indirect symbol. Also used for copying flags to a weakdef. */
6973 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6974 struct elf_link_hash_entry *dir,
6975 struct elf_link_hash_entry *ind)
6977 struct elf_link_hash_table *htab;
6979 /* Copy down any references that we may have already seen to the
6980 symbol which just became indirect if DIR isn't a hidden versioned
6983 if (dir->versioned != versioned_hidden)
6985 dir->ref_dynamic |= ind->ref_dynamic;
6986 dir->ref_regular |= ind->ref_regular;
6987 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6988 dir->non_got_ref |= ind->non_got_ref;
6989 dir->needs_plt |= ind->needs_plt;
6990 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6993 if (ind->root.type != bfd_link_hash_indirect)
6996 /* Copy over the global and procedure linkage table refcount entries.
6997 These may have been already set up by a check_relocs routine. */
6998 htab = elf_hash_table (info);
6999 if (ind->got.refcount > htab->init_got_refcount.refcount)
7001 if (dir->got.refcount < 0)
7002 dir->got.refcount = 0;
7003 dir->got.refcount += ind->got.refcount;
7004 ind->got.refcount = htab->init_got_refcount.refcount;
7007 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7009 if (dir->plt.refcount < 0)
7010 dir->plt.refcount = 0;
7011 dir->plt.refcount += ind->plt.refcount;
7012 ind->plt.refcount = htab->init_plt_refcount.refcount;
7015 if (ind->dynindx != -1)
7017 if (dir->dynindx != -1)
7018 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7019 dir->dynindx = ind->dynindx;
7020 dir->dynstr_index = ind->dynstr_index;
7022 ind->dynstr_index = 0;
7027 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7028 struct elf_link_hash_entry *h,
7029 bfd_boolean force_local)
7031 /* STT_GNU_IFUNC symbol must go through PLT. */
7032 if (h->type != STT_GNU_IFUNC)
7034 h->plt = elf_hash_table (info)->init_plt_offset;
7039 h->forced_local = 1;
7040 if (h->dynindx != -1)
7043 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7049 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7053 _bfd_elf_link_hash_table_init
7054 (struct elf_link_hash_table *table,
7056 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7057 struct bfd_hash_table *,
7059 unsigned int entsize,
7060 enum elf_target_id target_id)
7063 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7065 table->init_got_refcount.refcount = can_refcount - 1;
7066 table->init_plt_refcount.refcount = can_refcount - 1;
7067 table->init_got_offset.offset = -(bfd_vma) 1;
7068 table->init_plt_offset.offset = -(bfd_vma) 1;
7069 /* The first dynamic symbol is a dummy. */
7070 table->dynsymcount = 1;
7072 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7074 table->root.type = bfd_link_elf_hash_table;
7075 table->hash_table_id = target_id;
7080 /* Create an ELF linker hash table. */
7082 struct bfd_link_hash_table *
7083 _bfd_elf_link_hash_table_create (bfd *abfd)
7085 struct elf_link_hash_table *ret;
7086 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7088 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7092 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7093 sizeof (struct elf_link_hash_entry),
7099 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7104 /* Destroy an ELF linker hash table. */
7107 _bfd_elf_link_hash_table_free (bfd *obfd)
7109 struct elf_link_hash_table *htab;
7111 htab = (struct elf_link_hash_table *) obfd->link.hash;
7112 if (htab->dynstr != NULL)
7113 _bfd_elf_strtab_free (htab->dynstr);
7114 _bfd_merge_sections_free (htab->merge_info);
7115 _bfd_generic_link_hash_table_free (obfd);
7118 /* This is a hook for the ELF emulation code in the generic linker to
7119 tell the backend linker what file name to use for the DT_NEEDED
7120 entry for a dynamic object. */
7123 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7125 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7126 && bfd_get_format (abfd) == bfd_object)
7127 elf_dt_name (abfd) = name;
7131 bfd_elf_get_dyn_lib_class (bfd *abfd)
7134 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7135 && bfd_get_format (abfd) == bfd_object)
7136 lib_class = elf_dyn_lib_class (abfd);
7143 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7145 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7146 && bfd_get_format (abfd) == bfd_object)
7147 elf_dyn_lib_class (abfd) = lib_class;
7150 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7151 the linker ELF emulation code. */
7153 struct bfd_link_needed_list *
7154 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7155 struct bfd_link_info *info)
7157 if (! is_elf_hash_table (info->hash))
7159 return elf_hash_table (info)->needed;
7162 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7163 hook for the linker ELF emulation code. */
7165 struct bfd_link_needed_list *
7166 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7167 struct bfd_link_info *info)
7169 if (! is_elf_hash_table (info->hash))
7171 return elf_hash_table (info)->runpath;
7174 /* Get the name actually used for a dynamic object for a link. This
7175 is the SONAME entry if there is one. Otherwise, it is the string
7176 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7179 bfd_elf_get_dt_soname (bfd *abfd)
7181 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7182 && bfd_get_format (abfd) == bfd_object)
7183 return elf_dt_name (abfd);
7187 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7188 the ELF linker emulation code. */
7191 bfd_elf_get_bfd_needed_list (bfd *abfd,
7192 struct bfd_link_needed_list **pneeded)
7195 bfd_byte *dynbuf = NULL;
7196 unsigned int elfsec;
7197 unsigned long shlink;
7198 bfd_byte *extdyn, *extdynend;
7200 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7204 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7205 || bfd_get_format (abfd) != bfd_object)
7208 s = bfd_get_section_by_name (abfd, ".dynamic");
7209 if (s == NULL || s->size == 0)
7212 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7215 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7216 if (elfsec == SHN_BAD)
7219 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7221 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7222 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7225 extdynend = extdyn + s->size;
7226 for (; extdyn < extdynend; extdyn += extdynsize)
7228 Elf_Internal_Dyn dyn;
7230 (*swap_dyn_in) (abfd, extdyn, &dyn);
7232 if (dyn.d_tag == DT_NULL)
7235 if (dyn.d_tag == DT_NEEDED)
7238 struct bfd_link_needed_list *l;
7239 unsigned int tagv = dyn.d_un.d_val;
7242 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7247 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7268 struct elf_symbuf_symbol
7270 unsigned long st_name; /* Symbol name, index in string tbl */
7271 unsigned char st_info; /* Type and binding attributes */
7272 unsigned char st_other; /* Visibilty, and target specific */
7275 struct elf_symbuf_head
7277 struct elf_symbuf_symbol *ssym;
7278 bfd_size_type count;
7279 unsigned int st_shndx;
7286 Elf_Internal_Sym *isym;
7287 struct elf_symbuf_symbol *ssym;
7292 /* Sort references to symbols by ascending section number. */
7295 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7297 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7298 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7300 return s1->st_shndx - s2->st_shndx;
7304 elf_sym_name_compare (const void *arg1, const void *arg2)
7306 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7307 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7308 return strcmp (s1->name, s2->name);
7311 static struct elf_symbuf_head *
7312 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7314 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7315 struct elf_symbuf_symbol *ssym;
7316 struct elf_symbuf_head *ssymbuf, *ssymhead;
7317 bfd_size_type i, shndx_count, total_size;
7319 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7323 for (ind = indbuf, i = 0; i < symcount; i++)
7324 if (isymbuf[i].st_shndx != SHN_UNDEF)
7325 *ind++ = &isymbuf[i];
7328 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7329 elf_sort_elf_symbol);
7332 if (indbufend > indbuf)
7333 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7334 if (ind[0]->st_shndx != ind[1]->st_shndx)
7337 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7338 + (indbufend - indbuf) * sizeof (*ssym));
7339 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7340 if (ssymbuf == NULL)
7346 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7347 ssymbuf->ssym = NULL;
7348 ssymbuf->count = shndx_count;
7349 ssymbuf->st_shndx = 0;
7350 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7352 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7355 ssymhead->ssym = ssym;
7356 ssymhead->count = 0;
7357 ssymhead->st_shndx = (*ind)->st_shndx;
7359 ssym->st_name = (*ind)->st_name;
7360 ssym->st_info = (*ind)->st_info;
7361 ssym->st_other = (*ind)->st_other;
7364 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7365 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7372 /* Check if 2 sections define the same set of local and global
7376 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7377 struct bfd_link_info *info)
7380 const struct elf_backend_data *bed1, *bed2;
7381 Elf_Internal_Shdr *hdr1, *hdr2;
7382 bfd_size_type symcount1, symcount2;
7383 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7384 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7385 Elf_Internal_Sym *isym, *isymend;
7386 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7387 bfd_size_type count1, count2, i;
7388 unsigned int shndx1, shndx2;
7394 /* Both sections have to be in ELF. */
7395 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7396 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7399 if (elf_section_type (sec1) != elf_section_type (sec2))
7402 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7403 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7404 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7407 bed1 = get_elf_backend_data (bfd1);
7408 bed2 = get_elf_backend_data (bfd2);
7409 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7410 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7411 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7412 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7414 if (symcount1 == 0 || symcount2 == 0)
7420 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7421 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7423 if (ssymbuf1 == NULL)
7425 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7427 if (isymbuf1 == NULL)
7430 if (!info->reduce_memory_overheads)
7431 elf_tdata (bfd1)->symbuf = ssymbuf1
7432 = elf_create_symbuf (symcount1, isymbuf1);
7435 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7437 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7439 if (isymbuf2 == NULL)
7442 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7443 elf_tdata (bfd2)->symbuf = ssymbuf2
7444 = elf_create_symbuf (symcount2, isymbuf2);
7447 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7449 /* Optimized faster version. */
7450 bfd_size_type lo, hi, mid;
7451 struct elf_symbol *symp;
7452 struct elf_symbuf_symbol *ssym, *ssymend;
7455 hi = ssymbuf1->count;
7460 mid = (lo + hi) / 2;
7461 if (shndx1 < ssymbuf1[mid].st_shndx)
7463 else if (shndx1 > ssymbuf1[mid].st_shndx)
7467 count1 = ssymbuf1[mid].count;
7474 hi = ssymbuf2->count;
7479 mid = (lo + hi) / 2;
7480 if (shndx2 < ssymbuf2[mid].st_shndx)
7482 else if (shndx2 > ssymbuf2[mid].st_shndx)
7486 count2 = ssymbuf2[mid].count;
7492 if (count1 == 0 || count2 == 0 || count1 != count2)
7496 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7498 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7499 if (symtable1 == NULL || symtable2 == NULL)
7503 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7504 ssym < ssymend; ssym++, symp++)
7506 symp->u.ssym = ssym;
7507 symp->name = bfd_elf_string_from_elf_section (bfd1,
7513 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7514 ssym < ssymend; ssym++, symp++)
7516 symp->u.ssym = ssym;
7517 symp->name = bfd_elf_string_from_elf_section (bfd2,
7522 /* Sort symbol by name. */
7523 qsort (symtable1, count1, sizeof (struct elf_symbol),
7524 elf_sym_name_compare);
7525 qsort (symtable2, count1, sizeof (struct elf_symbol),
7526 elf_sym_name_compare);
7528 for (i = 0; i < count1; i++)
7529 /* Two symbols must have the same binding, type and name. */
7530 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7531 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7532 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7539 symtable1 = (struct elf_symbol *)
7540 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7541 symtable2 = (struct elf_symbol *)
7542 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7543 if (symtable1 == NULL || symtable2 == NULL)
7546 /* Count definitions in the section. */
7548 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7549 if (isym->st_shndx == shndx1)
7550 symtable1[count1++].u.isym = isym;
7553 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7554 if (isym->st_shndx == shndx2)
7555 symtable2[count2++].u.isym = isym;
7557 if (count1 == 0 || count2 == 0 || count1 != count2)
7560 for (i = 0; i < count1; i++)
7562 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7563 symtable1[i].u.isym->st_name);
7565 for (i = 0; i < count2; i++)
7567 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7568 symtable2[i].u.isym->st_name);
7570 /* Sort symbol by name. */
7571 qsort (symtable1, count1, sizeof (struct elf_symbol),
7572 elf_sym_name_compare);
7573 qsort (symtable2, count1, sizeof (struct elf_symbol),
7574 elf_sym_name_compare);
7576 for (i = 0; i < count1; i++)
7577 /* Two symbols must have the same binding, type and name. */
7578 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7579 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7580 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7598 /* Return TRUE if 2 section types are compatible. */
7601 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7602 bfd *bbfd, const asection *bsec)
7606 || abfd->xvec->flavour != bfd_target_elf_flavour
7607 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7610 return elf_section_type (asec) == elf_section_type (bsec);
7613 /* Final phase of ELF linker. */
7615 /* A structure we use to avoid passing large numbers of arguments. */
7617 struct elf_final_link_info
7619 /* General link information. */
7620 struct bfd_link_info *info;
7623 /* Symbol string table. */
7624 struct elf_strtab_hash *symstrtab;
7625 /* .hash section. */
7627 /* symbol version section (.gnu.version). */
7628 asection *symver_sec;
7629 /* Buffer large enough to hold contents of any section. */
7631 /* Buffer large enough to hold external relocs of any section. */
7632 void *external_relocs;
7633 /* Buffer large enough to hold internal relocs of any section. */
7634 Elf_Internal_Rela *internal_relocs;
7635 /* Buffer large enough to hold external local symbols of any input
7637 bfd_byte *external_syms;
7638 /* And a buffer for symbol section indices. */
7639 Elf_External_Sym_Shndx *locsym_shndx;
7640 /* Buffer large enough to hold internal local symbols of any input
7642 Elf_Internal_Sym *internal_syms;
7643 /* Array large enough to hold a symbol index for each local symbol
7644 of any input BFD. */
7646 /* Array large enough to hold a section pointer for each local
7647 symbol of any input BFD. */
7648 asection **sections;
7649 /* Buffer for SHT_SYMTAB_SHNDX section. */
7650 Elf_External_Sym_Shndx *symshndxbuf;
7651 /* Number of STT_FILE syms seen. */
7652 size_t filesym_count;
7655 /* This struct is used to pass information to elf_link_output_extsym. */
7657 struct elf_outext_info
7660 bfd_boolean localsyms;
7661 bfd_boolean file_sym_done;
7662 struct elf_final_link_info *flinfo;
7666 /* Support for evaluating a complex relocation.
7668 Complex relocations are generalized, self-describing relocations. The
7669 implementation of them consists of two parts: complex symbols, and the
7670 relocations themselves.
7672 The relocations are use a reserved elf-wide relocation type code (R_RELC
7673 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7674 information (start bit, end bit, word width, etc) into the addend. This
7675 information is extracted from CGEN-generated operand tables within gas.
7677 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7678 internal) representing prefix-notation expressions, including but not
7679 limited to those sorts of expressions normally encoded as addends in the
7680 addend field. The symbol mangling format is:
7683 | <unary-operator> ':' <node>
7684 | <binary-operator> ':' <node> ':' <node>
7687 <literal> := 's' <digits=N> ':' <N character symbol name>
7688 | 'S' <digits=N> ':' <N character section name>
7692 <binary-operator> := as in C
7693 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7696 set_symbol_value (bfd *bfd_with_globals,
7697 Elf_Internal_Sym *isymbuf,
7702 struct elf_link_hash_entry **sym_hashes;
7703 struct elf_link_hash_entry *h;
7704 size_t extsymoff = locsymcount;
7706 if (symidx < locsymcount)
7708 Elf_Internal_Sym *sym;
7710 sym = isymbuf + symidx;
7711 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7713 /* It is a local symbol: move it to the
7714 "absolute" section and give it a value. */
7715 sym->st_shndx = SHN_ABS;
7716 sym->st_value = val;
7719 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7723 /* It is a global symbol: set its link type
7724 to "defined" and give it a value. */
7726 sym_hashes = elf_sym_hashes (bfd_with_globals);
7727 h = sym_hashes [symidx - extsymoff];
7728 while (h->root.type == bfd_link_hash_indirect
7729 || h->root.type == bfd_link_hash_warning)
7730 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7731 h->root.type = bfd_link_hash_defined;
7732 h->root.u.def.value = val;
7733 h->root.u.def.section = bfd_abs_section_ptr;
7737 resolve_symbol (const char *name,
7739 struct elf_final_link_info *flinfo,
7741 Elf_Internal_Sym *isymbuf,
7744 Elf_Internal_Sym *sym;
7745 struct bfd_link_hash_entry *global_entry;
7746 const char *candidate = NULL;
7747 Elf_Internal_Shdr *symtab_hdr;
7750 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7752 for (i = 0; i < locsymcount; ++ i)
7756 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7759 candidate = bfd_elf_string_from_elf_section (input_bfd,
7760 symtab_hdr->sh_link,
7763 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7764 name, candidate, (unsigned long) sym->st_value);
7766 if (candidate && strcmp (candidate, name) == 0)
7768 asection *sec = flinfo->sections [i];
7770 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7771 *result += sec->output_offset + sec->output_section->vma;
7773 printf ("Found symbol with value %8.8lx\n",
7774 (unsigned long) *result);
7780 /* Hmm, haven't found it yet. perhaps it is a global. */
7781 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7782 FALSE, FALSE, TRUE);
7786 if (global_entry->type == bfd_link_hash_defined
7787 || global_entry->type == bfd_link_hash_defweak)
7789 *result = (global_entry->u.def.value
7790 + global_entry->u.def.section->output_section->vma
7791 + global_entry->u.def.section->output_offset);
7793 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7794 global_entry->root.string, (unsigned long) *result);
7802 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7803 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7804 names like "foo.end" which is the end address of section "foo". */
7807 resolve_section (const char *name,
7815 for (curr = sections; curr; curr = curr->next)
7816 if (strcmp (curr->name, name) == 0)
7818 *result = curr->vma;
7822 /* Hmm. still haven't found it. try pseudo-section names. */
7823 /* FIXME: This could be coded more efficiently... */
7824 for (curr = sections; curr; curr = curr->next)
7826 len = strlen (curr->name);
7827 if (len > strlen (name))
7830 if (strncmp (curr->name, name, len) == 0)
7832 if (strncmp (".end", name + len, 4) == 0)
7834 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7838 /* Insert more pseudo-section names here, if you like. */
7846 undefined_reference (const char *reftype, const char *name)
7848 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7853 eval_symbol (bfd_vma *result,
7856 struct elf_final_link_info *flinfo,
7858 Elf_Internal_Sym *isymbuf,
7867 const char *sym = *symp;
7869 bfd_boolean symbol_is_section = FALSE;
7874 if (len < 1 || len > sizeof (symbuf))
7876 bfd_set_error (bfd_error_invalid_operation);
7889 *result = strtoul (sym, (char **) symp, 16);
7893 symbol_is_section = TRUE;
7896 symlen = strtol (sym, (char **) symp, 10);
7897 sym = *symp + 1; /* Skip the trailing ':'. */
7899 if (symend < sym || symlen + 1 > sizeof (symbuf))
7901 bfd_set_error (bfd_error_invalid_operation);
7905 memcpy (symbuf, sym, symlen);
7906 symbuf[symlen] = '\0';
7907 *symp = sym + symlen;
7909 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7910 the symbol as a section, or vice-versa. so we're pretty liberal in our
7911 interpretation here; section means "try section first", not "must be a
7912 section", and likewise with symbol. */
7914 if (symbol_is_section)
7916 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
7917 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7918 isymbuf, locsymcount))
7920 undefined_reference ("section", symbuf);
7926 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7927 isymbuf, locsymcount)
7928 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7931 undefined_reference ("symbol", symbuf);
7938 /* All that remains are operators. */
7940 #define UNARY_OP(op) \
7941 if (strncmp (sym, #op, strlen (#op)) == 0) \
7943 sym += strlen (#op); \
7947 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7948 isymbuf, locsymcount, signed_p)) \
7951 *result = op ((bfd_signed_vma) a); \
7957 #define BINARY_OP(op) \
7958 if (strncmp (sym, #op, strlen (#op)) == 0) \
7960 sym += strlen (#op); \
7964 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7965 isymbuf, locsymcount, signed_p)) \
7968 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7969 isymbuf, locsymcount, signed_p)) \
7972 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8002 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8003 bfd_set_error (bfd_error_invalid_operation);
8009 put_value (bfd_vma size,
8010 unsigned long chunksz,
8015 location += (size - chunksz);
8017 for (; size; size -= chunksz, location -= chunksz)
8022 bfd_put_8 (input_bfd, x, location);
8026 bfd_put_16 (input_bfd, x, location);
8030 bfd_put_32 (input_bfd, x, location);
8031 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8037 bfd_put_64 (input_bfd, x, location);
8038 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8051 get_value (bfd_vma size,
8052 unsigned long chunksz,
8059 /* Sanity checks. */
8060 BFD_ASSERT (chunksz <= sizeof (x)
8063 && (size % chunksz) == 0
8064 && input_bfd != NULL
8065 && location != NULL);
8067 if (chunksz == sizeof (x))
8069 BFD_ASSERT (size == chunksz);
8071 /* Make sure that we do not perform an undefined shift operation.
8072 We know that size == chunksz so there will only be one iteration
8073 of the loop below. */
8077 shift = 8 * chunksz;
8079 for (; size; size -= chunksz, location += chunksz)
8084 x = (x << shift) | bfd_get_8 (input_bfd, location);
8087 x = (x << shift) | bfd_get_16 (input_bfd, location);
8090 x = (x << shift) | bfd_get_32 (input_bfd, location);
8094 x = (x << shift) | bfd_get_64 (input_bfd, location);
8105 decode_complex_addend (unsigned long *start, /* in bits */
8106 unsigned long *oplen, /* in bits */
8107 unsigned long *len, /* in bits */
8108 unsigned long *wordsz, /* in bytes */
8109 unsigned long *chunksz, /* in bytes */
8110 unsigned long *lsb0_p,
8111 unsigned long *signed_p,
8112 unsigned long *trunc_p,
8113 unsigned long encoded)
8115 * start = encoded & 0x3F;
8116 * len = (encoded >> 6) & 0x3F;
8117 * oplen = (encoded >> 12) & 0x3F;
8118 * wordsz = (encoded >> 18) & 0xF;
8119 * chunksz = (encoded >> 22) & 0xF;
8120 * lsb0_p = (encoded >> 27) & 1;
8121 * signed_p = (encoded >> 28) & 1;
8122 * trunc_p = (encoded >> 29) & 1;
8125 bfd_reloc_status_type
8126 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8127 asection *input_section ATTRIBUTE_UNUSED,
8129 Elf_Internal_Rela *rel,
8132 bfd_vma shift, x, mask;
8133 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8134 bfd_reloc_status_type r;
8136 /* Perform this reloc, since it is complex.
8137 (this is not to say that it necessarily refers to a complex
8138 symbol; merely that it is a self-describing CGEN based reloc.
8139 i.e. the addend has the complete reloc information (bit start, end,
8140 word size, etc) encoded within it.). */
8142 decode_complex_addend (&start, &oplen, &len, &wordsz,
8143 &chunksz, &lsb0_p, &signed_p,
8144 &trunc_p, rel->r_addend);
8146 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8149 shift = (start + 1) - len;
8151 shift = (8 * wordsz) - (start + len);
8153 x = get_value (wordsz, chunksz, input_bfd,
8154 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8157 printf ("Doing complex reloc: "
8158 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8159 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8160 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8161 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8162 oplen, (unsigned long) x, (unsigned long) mask,
8163 (unsigned long) relocation);
8168 /* Now do an overflow check. */
8169 r = bfd_check_overflow ((signed_p
8170 ? complain_overflow_signed
8171 : complain_overflow_unsigned),
8172 len, 0, (8 * wordsz),
8176 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8179 printf (" relocation: %8.8lx\n"
8180 " shifted mask: %8.8lx\n"
8181 " shifted/masked reloc: %8.8lx\n"
8182 " result: %8.8lx\n",
8183 (unsigned long) relocation, (unsigned long) (mask << shift),
8184 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8186 put_value (wordsz, chunksz, input_bfd, x,
8187 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8191 /* Functions to read r_offset from external (target order) reloc
8192 entry. Faster than bfd_getl32 et al, because we let the compiler
8193 know the value is aligned. */
8196 ext32l_r_offset (const void *p)
8203 const union aligned32 *a
8204 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8206 uint32_t aval = ( (uint32_t) a->c[0]
8207 | (uint32_t) a->c[1] << 8
8208 | (uint32_t) a->c[2] << 16
8209 | (uint32_t) a->c[3] << 24);
8214 ext32b_r_offset (const void *p)
8221 const union aligned32 *a
8222 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8224 uint32_t aval = ( (uint32_t) a->c[0] << 24
8225 | (uint32_t) a->c[1] << 16
8226 | (uint32_t) a->c[2] << 8
8227 | (uint32_t) a->c[3]);
8231 #ifdef BFD_HOST_64_BIT
8233 ext64l_r_offset (const void *p)
8240 const union aligned64 *a
8241 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8243 uint64_t aval = ( (uint64_t) a->c[0]
8244 | (uint64_t) a->c[1] << 8
8245 | (uint64_t) a->c[2] << 16
8246 | (uint64_t) a->c[3] << 24
8247 | (uint64_t) a->c[4] << 32
8248 | (uint64_t) a->c[5] << 40
8249 | (uint64_t) a->c[6] << 48
8250 | (uint64_t) a->c[7] << 56);
8255 ext64b_r_offset (const void *p)
8262 const union aligned64 *a
8263 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8265 uint64_t aval = ( (uint64_t) a->c[0] << 56
8266 | (uint64_t) a->c[1] << 48
8267 | (uint64_t) a->c[2] << 40
8268 | (uint64_t) a->c[3] << 32
8269 | (uint64_t) a->c[4] << 24
8270 | (uint64_t) a->c[5] << 16
8271 | (uint64_t) a->c[6] << 8
8272 | (uint64_t) a->c[7]);
8277 /* When performing a relocatable link, the input relocations are
8278 preserved. But, if they reference global symbols, the indices
8279 referenced must be updated. Update all the relocations found in
8283 elf_link_adjust_relocs (bfd *abfd,
8284 struct bfd_elf_section_reloc_data *reldata,
8288 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8290 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8291 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8292 bfd_vma r_type_mask;
8294 unsigned int count = reldata->count;
8295 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8297 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8299 swap_in = bed->s->swap_reloc_in;
8300 swap_out = bed->s->swap_reloc_out;
8302 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8304 swap_in = bed->s->swap_reloca_in;
8305 swap_out = bed->s->swap_reloca_out;
8310 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8313 if (bed->s->arch_size == 32)
8320 r_type_mask = 0xffffffff;
8324 erela = reldata->hdr->contents;
8325 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8327 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8330 if (*rel_hash == NULL)
8333 BFD_ASSERT ((*rel_hash)->indx >= 0);
8335 (*swap_in) (abfd, erela, irela);
8336 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8337 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8338 | (irela[j].r_info & r_type_mask));
8339 (*swap_out) (abfd, irela, erela);
8342 if (sort && count != 0)
8344 bfd_vma (*ext_r_off) (const void *);
8347 bfd_byte *base, *end, *p, *loc;
8348 bfd_byte *buf = NULL;
8350 if (bed->s->arch_size == 32)
8352 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8353 ext_r_off = ext32l_r_offset;
8354 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8355 ext_r_off = ext32b_r_offset;
8361 #ifdef BFD_HOST_64_BIT
8362 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8363 ext_r_off = ext64l_r_offset;
8364 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8365 ext_r_off = ext64b_r_offset;
8371 /* Must use a stable sort here. A modified insertion sort,
8372 since the relocs are mostly sorted already. */
8373 elt_size = reldata->hdr->sh_entsize;
8374 base = reldata->hdr->contents;
8375 end = base + count * elt_size;
8376 if (elt_size > sizeof (Elf64_External_Rela))
8379 /* Ensure the first element is lowest. This acts as a sentinel,
8380 speeding the main loop below. */
8381 r_off = (*ext_r_off) (base);
8382 for (p = loc = base; (p += elt_size) < end; )
8384 bfd_vma r_off2 = (*ext_r_off) (p);
8393 /* Don't just swap *base and *loc as that changes the order
8394 of the original base[0] and base[1] if they happen to
8395 have the same r_offset. */
8396 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8397 memcpy (onebuf, loc, elt_size);
8398 memmove (base + elt_size, base, loc - base);
8399 memcpy (base, onebuf, elt_size);
8402 for (p = base + elt_size; (p += elt_size) < end; )
8404 /* base to p is sorted, *p is next to insert. */
8405 r_off = (*ext_r_off) (p);
8406 /* Search the sorted region for location to insert. */
8408 while (r_off < (*ext_r_off) (loc))
8413 /* Chances are there is a run of relocs to insert here,
8414 from one of more input files. Files are not always
8415 linked in order due to the way elf_link_input_bfd is
8416 called. See pr17666. */
8417 size_t sortlen = p - loc;
8418 bfd_vma r_off2 = (*ext_r_off) (loc);
8419 size_t runlen = elt_size;
8420 size_t buf_size = 96 * 1024;
8421 while (p + runlen < end
8422 && (sortlen <= buf_size
8423 || runlen + elt_size <= buf_size)
8424 && r_off2 > (*ext_r_off) (p + runlen))
8428 buf = bfd_malloc (buf_size);
8432 if (runlen < sortlen)
8434 memcpy (buf, p, runlen);
8435 memmove (loc + runlen, loc, sortlen);
8436 memcpy (loc, buf, runlen);
8440 memcpy (buf, loc, sortlen);
8441 memmove (loc, p, runlen);
8442 memcpy (loc + runlen, buf, sortlen);
8444 p += runlen - elt_size;
8447 /* Hashes are no longer valid. */
8448 free (reldata->hashes);
8449 reldata->hashes = NULL;
8455 struct elf_link_sort_rela
8461 enum elf_reloc_type_class type;
8462 /* We use this as an array of size int_rels_per_ext_rel. */
8463 Elf_Internal_Rela rela[1];
8467 elf_link_sort_cmp1 (const void *A, const void *B)
8469 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8470 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8471 int relativea, relativeb;
8473 relativea = a->type == reloc_class_relative;
8474 relativeb = b->type == reloc_class_relative;
8476 if (relativea < relativeb)
8478 if (relativea > relativeb)
8480 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8482 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8484 if (a->rela->r_offset < b->rela->r_offset)
8486 if (a->rela->r_offset > b->rela->r_offset)
8492 elf_link_sort_cmp2 (const void *A, const void *B)
8494 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8495 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8497 if (a->type < b->type)
8499 if (a->type > b->type)
8501 if (a->u.offset < b->u.offset)
8503 if (a->u.offset > b->u.offset)
8505 if (a->rela->r_offset < b->rela->r_offset)
8507 if (a->rela->r_offset > b->rela->r_offset)
8513 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8515 asection *dynamic_relocs;
8518 bfd_size_type count, size;
8519 size_t i, ret, sort_elt, ext_size;
8520 bfd_byte *sort, *s_non_relative, *p;
8521 struct elf_link_sort_rela *sq;
8522 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8523 int i2e = bed->s->int_rels_per_ext_rel;
8524 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8525 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8526 struct bfd_link_order *lo;
8528 bfd_boolean use_rela;
8530 /* Find a dynamic reloc section. */
8531 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8532 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8533 if (rela_dyn != NULL && rela_dyn->size > 0
8534 && rel_dyn != NULL && rel_dyn->size > 0)
8536 bfd_boolean use_rela_initialised = FALSE;
8538 /* This is just here to stop gcc from complaining.
8539 It's initialization checking code is not perfect. */
8542 /* Both sections are present. Examine the sizes
8543 of the indirect sections to help us choose. */
8544 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8545 if (lo->type == bfd_indirect_link_order)
8547 asection *o = lo->u.indirect.section;
8549 if ((o->size % bed->s->sizeof_rela) == 0)
8551 if ((o->size % bed->s->sizeof_rel) == 0)
8552 /* Section size is divisible by both rel and rela sizes.
8553 It is of no help to us. */
8557 /* Section size is only divisible by rela. */
8558 if (use_rela_initialised && (use_rela == FALSE))
8561 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8562 bfd_set_error (bfd_error_invalid_operation);
8568 use_rela_initialised = TRUE;
8572 else if ((o->size % bed->s->sizeof_rel) == 0)
8574 /* Section size is only divisible by rel. */
8575 if (use_rela_initialised && (use_rela == TRUE))
8578 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8579 bfd_set_error (bfd_error_invalid_operation);
8585 use_rela_initialised = TRUE;
8590 /* The section size is not divisible by either - something is wrong. */
8592 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8593 bfd_set_error (bfd_error_invalid_operation);
8598 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8599 if (lo->type == bfd_indirect_link_order)
8601 asection *o = lo->u.indirect.section;
8603 if ((o->size % bed->s->sizeof_rela) == 0)
8605 if ((o->size % bed->s->sizeof_rel) == 0)
8606 /* Section size is divisible by both rel and rela sizes.
8607 It is of no help to us. */
8611 /* Section size is only divisible by rela. */
8612 if (use_rela_initialised && (use_rela == FALSE))
8615 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8616 bfd_set_error (bfd_error_invalid_operation);
8622 use_rela_initialised = TRUE;
8626 else if ((o->size % bed->s->sizeof_rel) == 0)
8628 /* Section size is only divisible by rel. */
8629 if (use_rela_initialised && (use_rela == TRUE))
8632 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8633 bfd_set_error (bfd_error_invalid_operation);
8639 use_rela_initialised = TRUE;
8644 /* The section size is not divisible by either - something is wrong. */
8646 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8647 bfd_set_error (bfd_error_invalid_operation);
8652 if (! use_rela_initialised)
8656 else if (rela_dyn != NULL && rela_dyn->size > 0)
8658 else if (rel_dyn != NULL && rel_dyn->size > 0)
8665 dynamic_relocs = rela_dyn;
8666 ext_size = bed->s->sizeof_rela;
8667 swap_in = bed->s->swap_reloca_in;
8668 swap_out = bed->s->swap_reloca_out;
8672 dynamic_relocs = rel_dyn;
8673 ext_size = bed->s->sizeof_rel;
8674 swap_in = bed->s->swap_reloc_in;
8675 swap_out = bed->s->swap_reloc_out;
8679 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8680 if (lo->type == bfd_indirect_link_order)
8681 size += lo->u.indirect.section->size;
8683 if (size != dynamic_relocs->size)
8686 sort_elt = (sizeof (struct elf_link_sort_rela)
8687 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8689 count = dynamic_relocs->size / ext_size;
8692 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8696 (*info->callbacks->warning)
8697 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8701 if (bed->s->arch_size == 32)
8702 r_sym_mask = ~(bfd_vma) 0xff;
8704 r_sym_mask = ~(bfd_vma) 0xffffffff;
8706 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8707 if (lo->type == bfd_indirect_link_order)
8709 bfd_byte *erel, *erelend;
8710 asection *o = lo->u.indirect.section;
8712 if (o->contents == NULL && o->size != 0)
8714 /* This is a reloc section that is being handled as a normal
8715 section. See bfd_section_from_shdr. We can't combine
8716 relocs in this case. */
8721 erelend = o->contents + o->size;
8722 /* FIXME: octets_per_byte. */
8723 p = sort + o->output_offset / ext_size * sort_elt;
8725 while (erel < erelend)
8727 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8729 (*swap_in) (abfd, erel, s->rela);
8730 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8731 s->u.sym_mask = r_sym_mask;
8737 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8739 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8741 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8742 if (s->type != reloc_class_relative)
8748 sq = (struct elf_link_sort_rela *) s_non_relative;
8749 for (; i < count; i++, p += sort_elt)
8751 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8752 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8754 sp->u.offset = sq->rela->r_offset;
8757 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8759 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8760 if (lo->type == bfd_indirect_link_order)
8762 bfd_byte *erel, *erelend;
8763 asection *o = lo->u.indirect.section;
8766 erelend = o->contents + o->size;
8767 /* FIXME: octets_per_byte. */
8768 p = sort + o->output_offset / ext_size * sort_elt;
8769 while (erel < erelend)
8771 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8772 (*swap_out) (abfd, s->rela, erel);
8779 *psec = dynamic_relocs;
8783 /* Add a symbol to the output symbol string table. */
8786 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8788 Elf_Internal_Sym *elfsym,
8789 asection *input_sec,
8790 struct elf_link_hash_entry *h)
8792 int (*output_symbol_hook)
8793 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8794 struct elf_link_hash_entry *);
8795 struct elf_link_hash_table *hash_table;
8796 const struct elf_backend_data *bed;
8797 bfd_size_type strtabsize;
8799 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8801 bed = get_elf_backend_data (flinfo->output_bfd);
8802 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8803 if (output_symbol_hook != NULL)
8805 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8812 || (input_sec->flags & SEC_EXCLUDE))
8813 elfsym->st_name = (unsigned long) -1;
8816 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8817 to get the final offset for st_name. */
8819 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8821 if (elfsym->st_name == (unsigned long) -1)
8825 hash_table = elf_hash_table (flinfo->info);
8826 strtabsize = hash_table->strtabsize;
8827 if (strtabsize <= hash_table->strtabcount)
8829 strtabsize += strtabsize;
8830 hash_table->strtabsize = strtabsize;
8831 strtabsize *= sizeof (*hash_table->strtab);
8833 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8835 if (hash_table->strtab == NULL)
8838 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8839 hash_table->strtab[hash_table->strtabcount].dest_index
8840 = hash_table->strtabcount;
8841 hash_table->strtab[hash_table->strtabcount].destshndx_index
8842 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8844 bfd_get_symcount (flinfo->output_bfd) += 1;
8845 hash_table->strtabcount += 1;
8850 /* Swap symbols out to the symbol table and flush the output symbols to
8854 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8856 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8857 bfd_size_type amt, i;
8858 const struct elf_backend_data *bed;
8860 Elf_Internal_Shdr *hdr;
8864 if (!hash_table->strtabcount)
8867 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8869 bed = get_elf_backend_data (flinfo->output_bfd);
8871 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8872 symbuf = (bfd_byte *) bfd_malloc (amt);
8876 if (flinfo->symshndxbuf)
8878 amt = (sizeof (Elf_External_Sym_Shndx)
8879 * (bfd_get_symcount (flinfo->output_bfd)));
8880 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8881 if (flinfo->symshndxbuf == NULL)
8888 for (i = 0; i < hash_table->strtabcount; i++)
8890 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8891 if (elfsym->sym.st_name == (unsigned long) -1)
8892 elfsym->sym.st_name = 0;
8895 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8896 elfsym->sym.st_name);
8897 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8898 ((bfd_byte *) symbuf
8899 + (elfsym->dest_index
8900 * bed->s->sizeof_sym)),
8901 (flinfo->symshndxbuf
8902 + elfsym->destshndx_index));
8905 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8906 pos = hdr->sh_offset + hdr->sh_size;
8907 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8908 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8909 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8911 hdr->sh_size += amt;
8919 free (hash_table->strtab);
8920 hash_table->strtab = NULL;
8925 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8928 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8930 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8931 && sym->st_shndx < SHN_LORESERVE)
8933 /* The gABI doesn't support dynamic symbols in output sections
8935 (*_bfd_error_handler)
8936 (_("%B: Too many sections: %d (>= %d)"),
8937 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8938 bfd_set_error (bfd_error_nonrepresentable_section);
8944 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8945 allowing an unsatisfied unversioned symbol in the DSO to match a
8946 versioned symbol that would normally require an explicit version.
8947 We also handle the case that a DSO references a hidden symbol
8948 which may be satisfied by a versioned symbol in another DSO. */
8951 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8952 const struct elf_backend_data *bed,
8953 struct elf_link_hash_entry *h)
8956 struct elf_link_loaded_list *loaded;
8958 if (!is_elf_hash_table (info->hash))
8961 /* Check indirect symbol. */
8962 while (h->root.type == bfd_link_hash_indirect)
8963 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8965 switch (h->root.type)
8971 case bfd_link_hash_undefined:
8972 case bfd_link_hash_undefweak:
8973 abfd = h->root.u.undef.abfd;
8974 if ((abfd->flags & DYNAMIC) == 0
8975 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8979 case bfd_link_hash_defined:
8980 case bfd_link_hash_defweak:
8981 abfd = h->root.u.def.section->owner;
8984 case bfd_link_hash_common:
8985 abfd = h->root.u.c.p->section->owner;
8988 BFD_ASSERT (abfd != NULL);
8990 for (loaded = elf_hash_table (info)->loaded;
8992 loaded = loaded->next)
8995 Elf_Internal_Shdr *hdr;
8996 bfd_size_type symcount;
8997 bfd_size_type extsymcount;
8998 bfd_size_type extsymoff;
8999 Elf_Internal_Shdr *versymhdr;
9000 Elf_Internal_Sym *isym;
9001 Elf_Internal_Sym *isymend;
9002 Elf_Internal_Sym *isymbuf;
9003 Elf_External_Versym *ever;
9004 Elf_External_Versym *extversym;
9006 input = loaded->abfd;
9008 /* We check each DSO for a possible hidden versioned definition. */
9010 || (input->flags & DYNAMIC) == 0
9011 || elf_dynversym (input) == 0)
9014 hdr = &elf_tdata (input)->dynsymtab_hdr;
9016 symcount = hdr->sh_size / bed->s->sizeof_sym;
9017 if (elf_bad_symtab (input))
9019 extsymcount = symcount;
9024 extsymcount = symcount - hdr->sh_info;
9025 extsymoff = hdr->sh_info;
9028 if (extsymcount == 0)
9031 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9033 if (isymbuf == NULL)
9036 /* Read in any version definitions. */
9037 versymhdr = &elf_tdata (input)->dynversym_hdr;
9038 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9039 if (extversym == NULL)
9042 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9043 || (bfd_bread (extversym, versymhdr->sh_size, input)
9044 != versymhdr->sh_size))
9052 ever = extversym + extsymoff;
9053 isymend = isymbuf + extsymcount;
9054 for (isym = isymbuf; isym < isymend; isym++, ever++)
9057 Elf_Internal_Versym iver;
9058 unsigned short version_index;
9060 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9061 || isym->st_shndx == SHN_UNDEF)
9064 name = bfd_elf_string_from_elf_section (input,
9067 if (strcmp (name, h->root.root.string) != 0)
9070 _bfd_elf_swap_versym_in (input, ever, &iver);
9072 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9074 && h->forced_local))
9076 /* If we have a non-hidden versioned sym, then it should
9077 have provided a definition for the undefined sym unless
9078 it is defined in a non-shared object and forced local.
9083 version_index = iver.vs_vers & VERSYM_VERSION;
9084 if (version_index == 1 || version_index == 2)
9086 /* This is the base or first version. We can use it. */
9100 /* Convert ELF common symbol TYPE. */
9103 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9105 /* Commom symbol can only appear in relocatable link. */
9106 if (!bfd_link_relocatable (info))
9108 switch (info->elf_stt_common)
9112 case elf_stt_common:
9115 case no_elf_stt_common:
9122 /* Add an external symbol to the symbol table. This is called from
9123 the hash table traversal routine. When generating a shared object,
9124 we go through the symbol table twice. The first time we output
9125 anything that might have been forced to local scope in a version
9126 script. The second time we output the symbols that are still
9130 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9132 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9133 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9134 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9136 Elf_Internal_Sym sym;
9137 asection *input_sec;
9138 const struct elf_backend_data *bed;
9142 /* A symbol is bound locally if it is forced local or it is locally
9143 defined, hidden versioned, not referenced by shared library and
9144 not exported when linking executable. */
9145 bfd_boolean local_bind = (h->forced_local
9146 || (bfd_link_executable (flinfo->info)
9147 && !flinfo->info->export_dynamic
9151 && h->versioned == versioned_hidden));
9153 if (h->root.type == bfd_link_hash_warning)
9155 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9156 if (h->root.type == bfd_link_hash_new)
9160 /* Decide whether to output this symbol in this pass. */
9161 if (eoinfo->localsyms)
9172 bed = get_elf_backend_data (flinfo->output_bfd);
9174 if (h->root.type == bfd_link_hash_undefined)
9176 /* If we have an undefined symbol reference here then it must have
9177 come from a shared library that is being linked in. (Undefined
9178 references in regular files have already been handled unless
9179 they are in unreferenced sections which are removed by garbage
9181 bfd_boolean ignore_undef = FALSE;
9183 /* Some symbols may be special in that the fact that they're
9184 undefined can be safely ignored - let backend determine that. */
9185 if (bed->elf_backend_ignore_undef_symbol)
9186 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9188 /* If we are reporting errors for this situation then do so now. */
9191 && (!h->ref_regular || flinfo->info->gc_sections)
9192 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9193 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9195 if (!(flinfo->info->callbacks->undefined_symbol
9196 (flinfo->info, h->root.root.string,
9197 h->ref_regular ? NULL : h->root.u.undef.abfd,
9199 (flinfo->info->unresolved_syms_in_shared_libs
9200 == RM_GENERATE_ERROR))))
9202 bfd_set_error (bfd_error_bad_value);
9203 eoinfo->failed = TRUE;
9209 /* We should also warn if a forced local symbol is referenced from
9210 shared libraries. */
9211 if (bfd_link_executable (flinfo->info)
9216 && h->ref_dynamic_nonweak
9217 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9221 struct elf_link_hash_entry *hi = h;
9223 /* Check indirect symbol. */
9224 while (hi->root.type == bfd_link_hash_indirect)
9225 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9227 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9228 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9229 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9230 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9232 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9233 def_bfd = flinfo->output_bfd;
9234 if (hi->root.u.def.section != bfd_abs_section_ptr)
9235 def_bfd = hi->root.u.def.section->owner;
9236 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9237 h->root.root.string);
9238 bfd_set_error (bfd_error_bad_value);
9239 eoinfo->failed = TRUE;
9243 /* We don't want to output symbols that have never been mentioned by
9244 a regular file, or that we have been told to strip. However, if
9245 h->indx is set to -2, the symbol is used by a reloc and we must
9250 else if ((h->def_dynamic
9252 || h->root.type == bfd_link_hash_new)
9256 else if (flinfo->info->strip == strip_all)
9258 else if (flinfo->info->strip == strip_some
9259 && bfd_hash_lookup (flinfo->info->keep_hash,
9260 h->root.root.string, FALSE, FALSE) == NULL)
9262 else if ((h->root.type == bfd_link_hash_defined
9263 || h->root.type == bfd_link_hash_defweak)
9264 && ((flinfo->info->strip_discarded
9265 && discarded_section (h->root.u.def.section))
9266 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9267 && h->root.u.def.section->owner != NULL
9268 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9270 else if ((h->root.type == bfd_link_hash_undefined
9271 || h->root.type == bfd_link_hash_undefweak)
9272 && h->root.u.undef.abfd != NULL
9273 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9278 /* If we're stripping it, and it's not a dynamic symbol, there's
9279 nothing else to do. However, if it is a forced local symbol or
9280 an ifunc symbol we need to give the backend finish_dynamic_symbol
9281 function a chance to make it dynamic. */
9284 && type != STT_GNU_IFUNC
9285 && !h->forced_local)
9289 sym.st_size = h->size;
9290 sym.st_other = h->other;
9291 switch (h->root.type)
9294 case bfd_link_hash_new:
9295 case bfd_link_hash_warning:
9299 case bfd_link_hash_undefined:
9300 case bfd_link_hash_undefweak:
9301 input_sec = bfd_und_section_ptr;
9302 sym.st_shndx = SHN_UNDEF;
9305 case bfd_link_hash_defined:
9306 case bfd_link_hash_defweak:
9308 input_sec = h->root.u.def.section;
9309 if (input_sec->output_section != NULL)
9312 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9313 input_sec->output_section);
9314 if (sym.st_shndx == SHN_BAD)
9316 (*_bfd_error_handler)
9317 (_("%B: could not find output section %A for input section %A"),
9318 flinfo->output_bfd, input_sec->output_section, input_sec);
9319 bfd_set_error (bfd_error_nonrepresentable_section);
9320 eoinfo->failed = TRUE;
9324 /* ELF symbols in relocatable files are section relative,
9325 but in nonrelocatable files they are virtual
9327 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9328 if (!bfd_link_relocatable (flinfo->info))
9330 sym.st_value += input_sec->output_section->vma;
9331 if (h->type == STT_TLS)
9333 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9334 if (tls_sec != NULL)
9335 sym.st_value -= tls_sec->vma;
9341 BFD_ASSERT (input_sec->owner == NULL
9342 || (input_sec->owner->flags & DYNAMIC) != 0);
9343 sym.st_shndx = SHN_UNDEF;
9344 input_sec = bfd_und_section_ptr;
9349 case bfd_link_hash_common:
9350 input_sec = h->root.u.c.p->section;
9351 sym.st_shndx = bed->common_section_index (input_sec);
9352 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9355 case bfd_link_hash_indirect:
9356 /* These symbols are created by symbol versioning. They point
9357 to the decorated version of the name. For example, if the
9358 symbol foo@@GNU_1.2 is the default, which should be used when
9359 foo is used with no version, then we add an indirect symbol
9360 foo which points to foo@@GNU_1.2. We ignore these symbols,
9361 since the indirected symbol is already in the hash table. */
9365 if (type == STT_COMMON || type == STT_OBJECT)
9366 switch (h->root.type)
9368 case bfd_link_hash_common:
9369 type = elf_link_convert_common_type (flinfo->info, type);
9371 case bfd_link_hash_defined:
9372 case bfd_link_hash_defweak:
9373 if (bed->common_definition (&sym))
9374 type = elf_link_convert_common_type (flinfo->info, type);
9378 case bfd_link_hash_undefined:
9379 case bfd_link_hash_undefweak:
9387 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9388 /* Turn off visibility on local symbol. */
9389 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9391 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9392 else if (h->unique_global && h->def_regular)
9393 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9394 else if (h->root.type == bfd_link_hash_undefweak
9395 || h->root.type == bfd_link_hash_defweak)
9396 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9398 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9399 sym.st_target_internal = h->target_internal;
9401 /* Give the processor backend a chance to tweak the symbol value,
9402 and also to finish up anything that needs to be done for this
9403 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9404 forced local syms when non-shared is due to a historical quirk.
9405 STT_GNU_IFUNC symbol must go through PLT. */
9406 if ((h->type == STT_GNU_IFUNC
9408 && !bfd_link_relocatable (flinfo->info))
9409 || ((h->dynindx != -1
9411 && ((bfd_link_pic (flinfo->info)
9412 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9413 || h->root.type != bfd_link_hash_undefweak))
9414 || !h->forced_local)
9415 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9417 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9418 (flinfo->output_bfd, flinfo->info, h, &sym)))
9420 eoinfo->failed = TRUE;
9425 /* If we are marking the symbol as undefined, and there are no
9426 non-weak references to this symbol from a regular object, then
9427 mark the symbol as weak undefined; if there are non-weak
9428 references, mark the symbol as strong. We can't do this earlier,
9429 because it might not be marked as undefined until the
9430 finish_dynamic_symbol routine gets through with it. */
9431 if (sym.st_shndx == SHN_UNDEF
9433 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9434 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9437 type = ELF_ST_TYPE (sym.st_info);
9439 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9440 if (type == STT_GNU_IFUNC)
9443 if (h->ref_regular_nonweak)
9444 bindtype = STB_GLOBAL;
9446 bindtype = STB_WEAK;
9447 sym.st_info = ELF_ST_INFO (bindtype, type);
9450 /* If this is a symbol defined in a dynamic library, don't use the
9451 symbol size from the dynamic library. Relinking an executable
9452 against a new library may introduce gratuitous changes in the
9453 executable's symbols if we keep the size. */
9454 if (sym.st_shndx == SHN_UNDEF
9459 /* If a non-weak symbol with non-default visibility is not defined
9460 locally, it is a fatal error. */
9461 if (!bfd_link_relocatable (flinfo->info)
9462 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9463 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9464 && h->root.type == bfd_link_hash_undefined
9469 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9470 msg = _("%B: protected symbol `%s' isn't defined");
9471 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9472 msg = _("%B: internal symbol `%s' isn't defined");
9474 msg = _("%B: hidden symbol `%s' isn't defined");
9475 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9476 bfd_set_error (bfd_error_bad_value);
9477 eoinfo->failed = TRUE;
9481 /* If this symbol should be put in the .dynsym section, then put it
9482 there now. We already know the symbol index. We also fill in
9483 the entry in the .hash section. */
9484 if (elf_hash_table (flinfo->info)->dynsym != NULL
9486 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9490 /* Since there is no version information in the dynamic string,
9491 if there is no version info in symbol version section, we will
9492 have a run-time problem if not linking executable, referenced
9493 by shared library, not locally defined, or not bound locally.
9495 if (h->verinfo.verdef == NULL
9497 && (!bfd_link_executable (flinfo->info)
9499 || !h->def_regular))
9501 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9503 if (p && p [1] != '\0')
9505 (*_bfd_error_handler)
9506 (_("%B: No symbol version section for versioned symbol `%s'"),
9507 flinfo->output_bfd, h->root.root.string);
9508 eoinfo->failed = TRUE;
9513 sym.st_name = h->dynstr_index;
9514 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9515 + h->dynindx * bed->s->sizeof_sym);
9516 if (!check_dynsym (flinfo->output_bfd, &sym))
9518 eoinfo->failed = TRUE;
9521 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9523 if (flinfo->hash_sec != NULL)
9525 size_t hash_entry_size;
9526 bfd_byte *bucketpos;
9531 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9532 bucket = h->u.elf_hash_value % bucketcount;
9535 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9536 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9537 + (bucket + 2) * hash_entry_size);
9538 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9539 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9541 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9542 ((bfd_byte *) flinfo->hash_sec->contents
9543 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9546 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9548 Elf_Internal_Versym iversym;
9549 Elf_External_Versym *eversym;
9551 if (!h->def_regular)
9553 if (h->verinfo.verdef == NULL
9554 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9555 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9556 iversym.vs_vers = 0;
9558 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9562 if (h->verinfo.vertree == NULL)
9563 iversym.vs_vers = 1;
9565 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9566 if (flinfo->info->create_default_symver)
9570 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9572 if (h->versioned == versioned_hidden && h->def_regular)
9573 iversym.vs_vers |= VERSYM_HIDDEN;
9575 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9576 eversym += h->dynindx;
9577 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9581 /* If the symbol is undefined, and we didn't output it to .dynsym,
9582 strip it from .symtab too. Obviously we can't do this for
9583 relocatable output or when needed for --emit-relocs. */
9584 else if (input_sec == bfd_und_section_ptr
9586 && !bfd_link_relocatable (flinfo->info))
9588 /* Also strip others that we couldn't earlier due to dynamic symbol
9592 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9595 /* Output a FILE symbol so that following locals are not associated
9596 with the wrong input file. We need one for forced local symbols
9597 if we've seen more than one FILE symbol or when we have exactly
9598 one FILE symbol but global symbols are present in a file other
9599 than the one with the FILE symbol. We also need one if linker
9600 defined symbols are present. In practice these conditions are
9601 always met, so just emit the FILE symbol unconditionally. */
9602 if (eoinfo->localsyms
9603 && !eoinfo->file_sym_done
9604 && eoinfo->flinfo->filesym_count != 0)
9606 Elf_Internal_Sym fsym;
9608 memset (&fsym, 0, sizeof (fsym));
9609 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9610 fsym.st_shndx = SHN_ABS;
9611 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9612 bfd_und_section_ptr, NULL))
9615 eoinfo->file_sym_done = TRUE;
9618 indx = bfd_get_symcount (flinfo->output_bfd);
9619 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9623 eoinfo->failed = TRUE;
9628 else if (h->indx == -2)
9634 /* Return TRUE if special handling is done for relocs in SEC against
9635 symbols defined in discarded sections. */
9638 elf_section_ignore_discarded_relocs (asection *sec)
9640 const struct elf_backend_data *bed;
9642 switch (sec->sec_info_type)
9644 case SEC_INFO_TYPE_STABS:
9645 case SEC_INFO_TYPE_EH_FRAME:
9646 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9652 bed = get_elf_backend_data (sec->owner);
9653 if (bed->elf_backend_ignore_discarded_relocs != NULL
9654 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9660 /* Return a mask saying how ld should treat relocations in SEC against
9661 symbols defined in discarded sections. If this function returns
9662 COMPLAIN set, ld will issue a warning message. If this function
9663 returns PRETEND set, and the discarded section was link-once and the
9664 same size as the kept link-once section, ld will pretend that the
9665 symbol was actually defined in the kept section. Otherwise ld will
9666 zero the reloc (at least that is the intent, but some cooperation by
9667 the target dependent code is needed, particularly for REL targets). */
9670 _bfd_elf_default_action_discarded (asection *sec)
9672 if (sec->flags & SEC_DEBUGGING)
9675 if (strcmp (".eh_frame", sec->name) == 0)
9678 if (strcmp (".gcc_except_table", sec->name) == 0)
9681 return COMPLAIN | PRETEND;
9684 /* Find a match between a section and a member of a section group. */
9687 match_group_member (asection *sec, asection *group,
9688 struct bfd_link_info *info)
9690 asection *first = elf_next_in_group (group);
9691 asection *s = first;
9695 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9698 s = elf_next_in_group (s);
9706 /* Check if the kept section of a discarded section SEC can be used
9707 to replace it. Return the replacement if it is OK. Otherwise return
9711 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9715 kept = sec->kept_section;
9718 if ((kept->flags & SEC_GROUP) != 0)
9719 kept = match_group_member (sec, kept, info);
9721 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9722 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9724 sec->kept_section = kept;
9729 /* Link an input file into the linker output file. This function
9730 handles all the sections and relocations of the input file at once.
9731 This is so that we only have to read the local symbols once, and
9732 don't have to keep them in memory. */
9735 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9737 int (*relocate_section)
9738 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9739 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9741 Elf_Internal_Shdr *symtab_hdr;
9744 Elf_Internal_Sym *isymbuf;
9745 Elf_Internal_Sym *isym;
9746 Elf_Internal_Sym *isymend;
9748 asection **ppsection;
9750 const struct elf_backend_data *bed;
9751 struct elf_link_hash_entry **sym_hashes;
9752 bfd_size_type address_size;
9753 bfd_vma r_type_mask;
9755 bfd_boolean have_file_sym = FALSE;
9757 output_bfd = flinfo->output_bfd;
9758 bed = get_elf_backend_data (output_bfd);
9759 relocate_section = bed->elf_backend_relocate_section;
9761 /* If this is a dynamic object, we don't want to do anything here:
9762 we don't want the local symbols, and we don't want the section
9764 if ((input_bfd->flags & DYNAMIC) != 0)
9767 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9768 if (elf_bad_symtab (input_bfd))
9770 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9775 locsymcount = symtab_hdr->sh_info;
9776 extsymoff = symtab_hdr->sh_info;
9779 /* Read the local symbols. */
9780 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9781 if (isymbuf == NULL && locsymcount != 0)
9783 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9784 flinfo->internal_syms,
9785 flinfo->external_syms,
9786 flinfo->locsym_shndx);
9787 if (isymbuf == NULL)
9791 /* Find local symbol sections and adjust values of symbols in
9792 SEC_MERGE sections. Write out those local symbols we know are
9793 going into the output file. */
9794 isymend = isymbuf + locsymcount;
9795 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9797 isym++, pindex++, ppsection++)
9801 Elf_Internal_Sym osym;
9807 if (elf_bad_symtab (input_bfd))
9809 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9816 if (isym->st_shndx == SHN_UNDEF)
9817 isec = bfd_und_section_ptr;
9818 else if (isym->st_shndx == SHN_ABS)
9819 isec = bfd_abs_section_ptr;
9820 else if (isym->st_shndx == SHN_COMMON)
9821 isec = bfd_com_section_ptr;
9824 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9827 /* Don't attempt to output symbols with st_shnx in the
9828 reserved range other than SHN_ABS and SHN_COMMON. */
9832 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9833 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9835 _bfd_merged_section_offset (output_bfd, &isec,
9836 elf_section_data (isec)->sec_info,
9842 /* Don't output the first, undefined, symbol. In fact, don't
9843 output any undefined local symbol. */
9844 if (isec == bfd_und_section_ptr)
9847 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9849 /* We never output section symbols. Instead, we use the
9850 section symbol of the corresponding section in the output
9855 /* If we are stripping all symbols, we don't want to output this
9857 if (flinfo->info->strip == strip_all)
9860 /* If we are discarding all local symbols, we don't want to
9861 output this one. If we are generating a relocatable output
9862 file, then some of the local symbols may be required by
9863 relocs; we output them below as we discover that they are
9865 if (flinfo->info->discard == discard_all)
9868 /* If this symbol is defined in a section which we are
9869 discarding, we don't need to keep it. */
9870 if (isym->st_shndx != SHN_UNDEF
9871 && isym->st_shndx < SHN_LORESERVE
9872 && bfd_section_removed_from_list (output_bfd,
9873 isec->output_section))
9876 /* Get the name of the symbol. */
9877 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9882 /* See if we are discarding symbols with this name. */
9883 if ((flinfo->info->strip == strip_some
9884 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9886 || (((flinfo->info->discard == discard_sec_merge
9887 && (isec->flags & SEC_MERGE)
9888 && !bfd_link_relocatable (flinfo->info))
9889 || flinfo->info->discard == discard_l)
9890 && bfd_is_local_label_name (input_bfd, name)))
9893 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9895 if (input_bfd->lto_output)
9896 /* -flto puts a temp file name here. This means builds
9897 are not reproducible. Discard the symbol. */
9899 have_file_sym = TRUE;
9900 flinfo->filesym_count += 1;
9904 /* In the absence of debug info, bfd_find_nearest_line uses
9905 FILE symbols to determine the source file for local
9906 function symbols. Provide a FILE symbol here if input
9907 files lack such, so that their symbols won't be
9908 associated with a previous input file. It's not the
9909 source file, but the best we can do. */
9910 have_file_sym = TRUE;
9911 flinfo->filesym_count += 1;
9912 memset (&osym, 0, sizeof (osym));
9913 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9914 osym.st_shndx = SHN_ABS;
9915 if (!elf_link_output_symstrtab (flinfo,
9916 (input_bfd->lto_output ? NULL
9917 : input_bfd->filename),
9918 &osym, bfd_abs_section_ptr,
9925 /* Adjust the section index for the output file. */
9926 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9927 isec->output_section);
9928 if (osym.st_shndx == SHN_BAD)
9931 /* ELF symbols in relocatable files are section relative, but
9932 in executable files they are virtual addresses. Note that
9933 this code assumes that all ELF sections have an associated
9934 BFD section with a reasonable value for output_offset; below
9935 we assume that they also have a reasonable value for
9936 output_section. Any special sections must be set up to meet
9937 these requirements. */
9938 osym.st_value += isec->output_offset;
9939 if (!bfd_link_relocatable (flinfo->info))
9941 osym.st_value += isec->output_section->vma;
9942 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9944 /* STT_TLS symbols are relative to PT_TLS segment base. */
9945 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9946 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9950 indx = bfd_get_symcount (output_bfd);
9951 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9958 if (bed->s->arch_size == 32)
9966 r_type_mask = 0xffffffff;
9971 /* Relocate the contents of each section. */
9972 sym_hashes = elf_sym_hashes (input_bfd);
9973 for (o = input_bfd->sections; o != NULL; o = o->next)
9977 if (! o->linker_mark)
9979 /* This section was omitted from the link. */
9983 if (bfd_link_relocatable (flinfo->info)
9984 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9986 /* Deal with the group signature symbol. */
9987 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9988 unsigned long symndx = sec_data->this_hdr.sh_info;
9989 asection *osec = o->output_section;
9991 if (symndx >= locsymcount
9992 || (elf_bad_symtab (input_bfd)
9993 && flinfo->sections[symndx] == NULL))
9995 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9996 while (h->root.type == bfd_link_hash_indirect
9997 || h->root.type == bfd_link_hash_warning)
9998 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9999 /* Arrange for symbol to be output. */
10001 elf_section_data (osec)->this_hdr.sh_info = -2;
10003 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10005 /* We'll use the output section target_index. */
10006 asection *sec = flinfo->sections[symndx]->output_section;
10007 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10011 if (flinfo->indices[symndx] == -1)
10013 /* Otherwise output the local symbol now. */
10014 Elf_Internal_Sym sym = isymbuf[symndx];
10015 asection *sec = flinfo->sections[symndx]->output_section;
10020 name = bfd_elf_string_from_elf_section (input_bfd,
10021 symtab_hdr->sh_link,
10026 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10028 if (sym.st_shndx == SHN_BAD)
10031 sym.st_value += o->output_offset;
10033 indx = bfd_get_symcount (output_bfd);
10034 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10039 flinfo->indices[symndx] = indx;
10043 elf_section_data (osec)->this_hdr.sh_info
10044 = flinfo->indices[symndx];
10048 if ((o->flags & SEC_HAS_CONTENTS) == 0
10049 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10052 if ((o->flags & SEC_LINKER_CREATED) != 0)
10054 /* Section was created by _bfd_elf_link_create_dynamic_sections
10059 /* Get the contents of the section. They have been cached by a
10060 relaxation routine. Note that o is a section in an input
10061 file, so the contents field will not have been set by any of
10062 the routines which work on output files. */
10063 if (elf_section_data (o)->this_hdr.contents != NULL)
10065 contents = elf_section_data (o)->this_hdr.contents;
10066 if (bed->caches_rawsize
10068 && o->rawsize < o->size)
10070 memcpy (flinfo->contents, contents, o->rawsize);
10071 contents = flinfo->contents;
10076 contents = flinfo->contents;
10077 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10081 if ((o->flags & SEC_RELOC) != 0)
10083 Elf_Internal_Rela *internal_relocs;
10084 Elf_Internal_Rela *rel, *relend;
10085 int action_discarded;
10088 /* Get the swapped relocs. */
10090 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10091 flinfo->internal_relocs, FALSE);
10092 if (internal_relocs == NULL
10093 && o->reloc_count > 0)
10096 /* We need to reverse-copy input .ctors/.dtors sections if
10097 they are placed in .init_array/.finit_array for output. */
10098 if (o->size > address_size
10099 && ((strncmp (o->name, ".ctors", 6) == 0
10100 && strcmp (o->output_section->name,
10101 ".init_array") == 0)
10102 || (strncmp (o->name, ".dtors", 6) == 0
10103 && strcmp (o->output_section->name,
10104 ".fini_array") == 0))
10105 && (o->name[6] == 0 || o->name[6] == '.'))
10107 if (o->size != o->reloc_count * address_size)
10109 (*_bfd_error_handler)
10110 (_("error: %B: size of section %A is not "
10111 "multiple of address size"),
10113 bfd_set_error (bfd_error_on_input);
10116 o->flags |= SEC_ELF_REVERSE_COPY;
10119 action_discarded = -1;
10120 if (!elf_section_ignore_discarded_relocs (o))
10121 action_discarded = (*bed->action_discarded) (o);
10123 /* Run through the relocs evaluating complex reloc symbols and
10124 looking for relocs against symbols from discarded sections
10125 or section symbols from removed link-once sections.
10126 Complain about relocs against discarded sections. Zero
10127 relocs against removed link-once sections. */
10129 rel = internal_relocs;
10130 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10131 for ( ; rel < relend; rel++)
10133 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10134 unsigned int s_type;
10135 asection **ps, *sec;
10136 struct elf_link_hash_entry *h = NULL;
10137 const char *sym_name;
10139 if (r_symndx == STN_UNDEF)
10142 if (r_symndx >= locsymcount
10143 || (elf_bad_symtab (input_bfd)
10144 && flinfo->sections[r_symndx] == NULL))
10146 h = sym_hashes[r_symndx - extsymoff];
10148 /* Badly formatted input files can contain relocs that
10149 reference non-existant symbols. Check here so that
10150 we do not seg fault. */
10155 sprintf_vma (buffer, rel->r_info);
10156 (*_bfd_error_handler)
10157 (_("error: %B contains a reloc (0x%s) for section %A "
10158 "that references a non-existent global symbol"),
10159 input_bfd, o, buffer);
10160 bfd_set_error (bfd_error_bad_value);
10164 while (h->root.type == bfd_link_hash_indirect
10165 || h->root.type == bfd_link_hash_warning)
10166 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10170 /* If a plugin symbol is referenced from a non-IR file,
10171 mark the symbol as undefined. Note that the
10172 linker may attach linker created dynamic sections
10173 to the plugin bfd. Symbols defined in linker
10174 created sections are not plugin symbols. */
10175 if (h->root.non_ir_ref
10176 && (h->root.type == bfd_link_hash_defined
10177 || h->root.type == bfd_link_hash_defweak)
10178 && (h->root.u.def.section->flags
10179 & SEC_LINKER_CREATED) == 0
10180 && h->root.u.def.section->owner != NULL
10181 && (h->root.u.def.section->owner->flags
10182 & BFD_PLUGIN) != 0)
10184 h->root.type = bfd_link_hash_undefined;
10185 h->root.u.undef.abfd = h->root.u.def.section->owner;
10189 if (h->root.type == bfd_link_hash_defined
10190 || h->root.type == bfd_link_hash_defweak)
10191 ps = &h->root.u.def.section;
10193 sym_name = h->root.root.string;
10197 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10199 s_type = ELF_ST_TYPE (sym->st_info);
10200 ps = &flinfo->sections[r_symndx];
10201 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10205 if ((s_type == STT_RELC || s_type == STT_SRELC)
10206 && !bfd_link_relocatable (flinfo->info))
10209 bfd_vma dot = (rel->r_offset
10210 + o->output_offset + o->output_section->vma);
10212 printf ("Encountered a complex symbol!");
10213 printf (" (input_bfd %s, section %s, reloc %ld\n",
10214 input_bfd->filename, o->name,
10215 (long) (rel - internal_relocs));
10216 printf (" symbol: idx %8.8lx, name %s\n",
10217 r_symndx, sym_name);
10218 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10219 (unsigned long) rel->r_info,
10220 (unsigned long) rel->r_offset);
10222 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10223 isymbuf, locsymcount, s_type == STT_SRELC))
10226 /* Symbol evaluated OK. Update to absolute value. */
10227 set_symbol_value (input_bfd, isymbuf, locsymcount,
10232 if (action_discarded != -1 && ps != NULL)
10234 /* Complain if the definition comes from a
10235 discarded section. */
10236 if ((sec = *ps) != NULL && discarded_section (sec))
10238 BFD_ASSERT (r_symndx != STN_UNDEF);
10239 if (action_discarded & COMPLAIN)
10240 (*flinfo->info->callbacks->einfo)
10241 (_("%X`%s' referenced in section `%A' of %B: "
10242 "defined in discarded section `%A' of %B\n"),
10243 sym_name, o, input_bfd, sec, sec->owner);
10245 /* Try to do the best we can to support buggy old
10246 versions of gcc. Pretend that the symbol is
10247 really defined in the kept linkonce section.
10248 FIXME: This is quite broken. Modifying the
10249 symbol here means we will be changing all later
10250 uses of the symbol, not just in this section. */
10251 if (action_discarded & PRETEND)
10255 kept = _bfd_elf_check_kept_section (sec,
10267 /* Relocate the section by invoking a back end routine.
10269 The back end routine is responsible for adjusting the
10270 section contents as necessary, and (if using Rela relocs
10271 and generating a relocatable output file) adjusting the
10272 reloc addend as necessary.
10274 The back end routine does not have to worry about setting
10275 the reloc address or the reloc symbol index.
10277 The back end routine is given a pointer to the swapped in
10278 internal symbols, and can access the hash table entries
10279 for the external symbols via elf_sym_hashes (input_bfd).
10281 When generating relocatable output, the back end routine
10282 must handle STB_LOCAL/STT_SECTION symbols specially. The
10283 output symbol is going to be a section symbol
10284 corresponding to the output section, which will require
10285 the addend to be adjusted. */
10287 ret = (*relocate_section) (output_bfd, flinfo->info,
10288 input_bfd, o, contents,
10296 || bfd_link_relocatable (flinfo->info)
10297 || flinfo->info->emitrelocations)
10299 Elf_Internal_Rela *irela;
10300 Elf_Internal_Rela *irelaend, *irelamid;
10301 bfd_vma last_offset;
10302 struct elf_link_hash_entry **rel_hash;
10303 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10304 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10305 unsigned int next_erel;
10306 bfd_boolean rela_normal;
10307 struct bfd_elf_section_data *esdi, *esdo;
10309 esdi = elf_section_data (o);
10310 esdo = elf_section_data (o->output_section);
10311 rela_normal = FALSE;
10313 /* Adjust the reloc addresses and symbol indices. */
10315 irela = internal_relocs;
10316 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10317 rel_hash = esdo->rel.hashes + esdo->rel.count;
10318 /* We start processing the REL relocs, if any. When we reach
10319 IRELAMID in the loop, we switch to the RELA relocs. */
10321 if (esdi->rel.hdr != NULL)
10322 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10323 * bed->s->int_rels_per_ext_rel);
10324 rel_hash_list = rel_hash;
10325 rela_hash_list = NULL;
10326 last_offset = o->output_offset;
10327 if (!bfd_link_relocatable (flinfo->info))
10328 last_offset += o->output_section->vma;
10329 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10331 unsigned long r_symndx;
10333 Elf_Internal_Sym sym;
10335 if (next_erel == bed->s->int_rels_per_ext_rel)
10341 if (irela == irelamid)
10343 rel_hash = esdo->rela.hashes + esdo->rela.count;
10344 rela_hash_list = rel_hash;
10345 rela_normal = bed->rela_normal;
10348 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10351 if (irela->r_offset >= (bfd_vma) -2)
10353 /* This is a reloc for a deleted entry or somesuch.
10354 Turn it into an R_*_NONE reloc, at the same
10355 offset as the last reloc. elf_eh_frame.c and
10356 bfd_elf_discard_info rely on reloc offsets
10358 irela->r_offset = last_offset;
10360 irela->r_addend = 0;
10364 irela->r_offset += o->output_offset;
10366 /* Relocs in an executable have to be virtual addresses. */
10367 if (!bfd_link_relocatable (flinfo->info))
10368 irela->r_offset += o->output_section->vma;
10370 last_offset = irela->r_offset;
10372 r_symndx = irela->r_info >> r_sym_shift;
10373 if (r_symndx == STN_UNDEF)
10376 if (r_symndx >= locsymcount
10377 || (elf_bad_symtab (input_bfd)
10378 && flinfo->sections[r_symndx] == NULL))
10380 struct elf_link_hash_entry *rh;
10381 unsigned long indx;
10383 /* This is a reloc against a global symbol. We
10384 have not yet output all the local symbols, so
10385 we do not know the symbol index of any global
10386 symbol. We set the rel_hash entry for this
10387 reloc to point to the global hash table entry
10388 for this symbol. The symbol index is then
10389 set at the end of bfd_elf_final_link. */
10390 indx = r_symndx - extsymoff;
10391 rh = elf_sym_hashes (input_bfd)[indx];
10392 while (rh->root.type == bfd_link_hash_indirect
10393 || rh->root.type == bfd_link_hash_warning)
10394 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10396 /* Setting the index to -2 tells
10397 elf_link_output_extsym that this symbol is
10398 used by a reloc. */
10399 BFD_ASSERT (rh->indx < 0);
10407 /* This is a reloc against a local symbol. */
10410 sym = isymbuf[r_symndx];
10411 sec = flinfo->sections[r_symndx];
10412 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10414 /* I suppose the backend ought to fill in the
10415 section of any STT_SECTION symbol against a
10416 processor specific section. */
10417 r_symndx = STN_UNDEF;
10418 if (bfd_is_abs_section (sec))
10420 else if (sec == NULL || sec->owner == NULL)
10422 bfd_set_error (bfd_error_bad_value);
10427 asection *osec = sec->output_section;
10429 /* If we have discarded a section, the output
10430 section will be the absolute section. In
10431 case of discarded SEC_MERGE sections, use
10432 the kept section. relocate_section should
10433 have already handled discarded linkonce
10435 if (bfd_is_abs_section (osec)
10436 && sec->kept_section != NULL
10437 && sec->kept_section->output_section != NULL)
10439 osec = sec->kept_section->output_section;
10440 irela->r_addend -= osec->vma;
10443 if (!bfd_is_abs_section (osec))
10445 r_symndx = osec->target_index;
10446 if (r_symndx == STN_UNDEF)
10448 irela->r_addend += osec->vma;
10449 osec = _bfd_nearby_section (output_bfd, osec,
10451 irela->r_addend -= osec->vma;
10452 r_symndx = osec->target_index;
10457 /* Adjust the addend according to where the
10458 section winds up in the output section. */
10460 irela->r_addend += sec->output_offset;
10464 if (flinfo->indices[r_symndx] == -1)
10466 unsigned long shlink;
10471 if (flinfo->info->strip == strip_all)
10473 /* You can't do ld -r -s. */
10474 bfd_set_error (bfd_error_invalid_operation);
10478 /* This symbol was skipped earlier, but
10479 since it is needed by a reloc, we
10480 must output it now. */
10481 shlink = symtab_hdr->sh_link;
10482 name = (bfd_elf_string_from_elf_section
10483 (input_bfd, shlink, sym.st_name));
10487 osec = sec->output_section;
10489 _bfd_elf_section_from_bfd_section (output_bfd,
10491 if (sym.st_shndx == SHN_BAD)
10494 sym.st_value += sec->output_offset;
10495 if (!bfd_link_relocatable (flinfo->info))
10497 sym.st_value += osec->vma;
10498 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10500 /* STT_TLS symbols are relative to PT_TLS
10502 BFD_ASSERT (elf_hash_table (flinfo->info)
10503 ->tls_sec != NULL);
10504 sym.st_value -= (elf_hash_table (flinfo->info)
10509 indx = bfd_get_symcount (output_bfd);
10510 ret = elf_link_output_symstrtab (flinfo, name,
10516 flinfo->indices[r_symndx] = indx;
10521 r_symndx = flinfo->indices[r_symndx];
10524 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10525 | (irela->r_info & r_type_mask));
10528 /* Swap out the relocs. */
10529 input_rel_hdr = esdi->rel.hdr;
10530 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10532 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10537 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10538 * bed->s->int_rels_per_ext_rel);
10539 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10542 input_rela_hdr = esdi->rela.hdr;
10543 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10545 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10554 /* Write out the modified section contents. */
10555 if (bed->elf_backend_write_section
10556 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10559 /* Section written out. */
10561 else switch (o->sec_info_type)
10563 case SEC_INFO_TYPE_STABS:
10564 if (! (_bfd_write_section_stabs
10566 &elf_hash_table (flinfo->info)->stab_info,
10567 o, &elf_section_data (o)->sec_info, contents)))
10570 case SEC_INFO_TYPE_MERGE:
10571 if (! _bfd_write_merged_section (output_bfd, o,
10572 elf_section_data (o)->sec_info))
10575 case SEC_INFO_TYPE_EH_FRAME:
10577 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10582 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10584 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10592 if (! (o->flags & SEC_EXCLUDE))
10594 file_ptr offset = (file_ptr) o->output_offset;
10595 bfd_size_type todo = o->size;
10597 offset *= bfd_octets_per_byte (output_bfd);
10599 if ((o->flags & SEC_ELF_REVERSE_COPY))
10601 /* Reverse-copy input section to output. */
10604 todo -= address_size;
10605 if (! bfd_set_section_contents (output_bfd,
10613 offset += address_size;
10617 else if (! bfd_set_section_contents (output_bfd,
10631 /* Generate a reloc when linking an ELF file. This is a reloc
10632 requested by the linker, and does not come from any input file. This
10633 is used to build constructor and destructor tables when linking
10637 elf_reloc_link_order (bfd *output_bfd,
10638 struct bfd_link_info *info,
10639 asection *output_section,
10640 struct bfd_link_order *link_order)
10642 reloc_howto_type *howto;
10646 struct bfd_elf_section_reloc_data *reldata;
10647 struct elf_link_hash_entry **rel_hash_ptr;
10648 Elf_Internal_Shdr *rel_hdr;
10649 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10650 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10653 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10655 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10658 bfd_set_error (bfd_error_bad_value);
10662 addend = link_order->u.reloc.p->addend;
10665 reldata = &esdo->rel;
10666 else if (esdo->rela.hdr)
10667 reldata = &esdo->rela;
10674 /* Figure out the symbol index. */
10675 rel_hash_ptr = reldata->hashes + reldata->count;
10676 if (link_order->type == bfd_section_reloc_link_order)
10678 indx = link_order->u.reloc.p->u.section->target_index;
10679 BFD_ASSERT (indx != 0);
10680 *rel_hash_ptr = NULL;
10684 struct elf_link_hash_entry *h;
10686 /* Treat a reloc against a defined symbol as though it were
10687 actually against the section. */
10688 h = ((struct elf_link_hash_entry *)
10689 bfd_wrapped_link_hash_lookup (output_bfd, info,
10690 link_order->u.reloc.p->u.name,
10691 FALSE, FALSE, TRUE));
10693 && (h->root.type == bfd_link_hash_defined
10694 || h->root.type == bfd_link_hash_defweak))
10698 section = h->root.u.def.section;
10699 indx = section->output_section->target_index;
10700 *rel_hash_ptr = NULL;
10701 /* It seems that we ought to add the symbol value to the
10702 addend here, but in practice it has already been added
10703 because it was passed to constructor_callback. */
10704 addend += section->output_section->vma + section->output_offset;
10706 else if (h != NULL)
10708 /* Setting the index to -2 tells elf_link_output_extsym that
10709 this symbol is used by a reloc. */
10716 if (! ((*info->callbacks->unattached_reloc)
10717 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10723 /* If this is an inplace reloc, we must write the addend into the
10725 if (howto->partial_inplace && addend != 0)
10727 bfd_size_type size;
10728 bfd_reloc_status_type rstat;
10731 const char *sym_name;
10733 size = (bfd_size_type) bfd_get_reloc_size (howto);
10734 buf = (bfd_byte *) bfd_zmalloc (size);
10735 if (buf == NULL && size != 0)
10737 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10744 case bfd_reloc_outofrange:
10747 case bfd_reloc_overflow:
10748 if (link_order->type == bfd_section_reloc_link_order)
10749 sym_name = bfd_section_name (output_bfd,
10750 link_order->u.reloc.p->u.section);
10752 sym_name = link_order->u.reloc.p->u.name;
10753 if (! ((*info->callbacks->reloc_overflow)
10754 (info, NULL, sym_name, howto->name, addend, NULL,
10755 NULL, (bfd_vma) 0)))
10763 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10765 * bfd_octets_per_byte (output_bfd),
10772 /* The address of a reloc is relative to the section in a
10773 relocatable file, and is a virtual address in an executable
10775 offset = link_order->offset;
10776 if (! bfd_link_relocatable (info))
10777 offset += output_section->vma;
10779 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10781 irel[i].r_offset = offset;
10782 irel[i].r_info = 0;
10783 irel[i].r_addend = 0;
10785 if (bed->s->arch_size == 32)
10786 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10788 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10790 rel_hdr = reldata->hdr;
10791 erel = rel_hdr->contents;
10792 if (rel_hdr->sh_type == SHT_REL)
10794 erel += reldata->count * bed->s->sizeof_rel;
10795 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10799 irel[0].r_addend = addend;
10800 erel += reldata->count * bed->s->sizeof_rela;
10801 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10810 /* Get the output vma of the section pointed to by the sh_link field. */
10813 elf_get_linked_section_vma (struct bfd_link_order *p)
10815 Elf_Internal_Shdr **elf_shdrp;
10819 s = p->u.indirect.section;
10820 elf_shdrp = elf_elfsections (s->owner);
10821 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10822 elfsec = elf_shdrp[elfsec]->sh_link;
10824 The Intel C compiler generates SHT_IA_64_UNWIND with
10825 SHF_LINK_ORDER. But it doesn't set the sh_link or
10826 sh_info fields. Hence we could get the situation
10827 where elfsec is 0. */
10830 const struct elf_backend_data *bed
10831 = get_elf_backend_data (s->owner);
10832 if (bed->link_order_error_handler)
10833 bed->link_order_error_handler
10834 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10839 s = elf_shdrp[elfsec]->bfd_section;
10840 return s->output_section->vma + s->output_offset;
10845 /* Compare two sections based on the locations of the sections they are
10846 linked to. Used by elf_fixup_link_order. */
10849 compare_link_order (const void * a, const void * b)
10854 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10855 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10858 return apos > bpos;
10862 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10863 order as their linked sections. Returns false if this could not be done
10864 because an output section includes both ordered and unordered
10865 sections. Ideally we'd do this in the linker proper. */
10868 elf_fixup_link_order (bfd *abfd, asection *o)
10870 int seen_linkorder;
10873 struct bfd_link_order *p;
10875 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10877 struct bfd_link_order **sections;
10878 asection *s, *other_sec, *linkorder_sec;
10882 linkorder_sec = NULL;
10884 seen_linkorder = 0;
10885 for (p = o->map_head.link_order; p != NULL; p = p->next)
10887 if (p->type == bfd_indirect_link_order)
10889 s = p->u.indirect.section;
10891 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10892 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10893 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10894 && elfsec < elf_numsections (sub)
10895 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10896 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10910 if (seen_other && seen_linkorder)
10912 if (other_sec && linkorder_sec)
10913 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10915 linkorder_sec->owner, other_sec,
10918 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10920 bfd_set_error (bfd_error_bad_value);
10925 if (!seen_linkorder)
10928 sections = (struct bfd_link_order **)
10929 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10930 if (sections == NULL)
10932 seen_linkorder = 0;
10934 for (p = o->map_head.link_order; p != NULL; p = p->next)
10936 sections[seen_linkorder++] = p;
10938 /* Sort the input sections in the order of their linked section. */
10939 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10940 compare_link_order);
10942 /* Change the offsets of the sections. */
10944 for (n = 0; n < seen_linkorder; n++)
10946 s = sections[n]->u.indirect.section;
10947 offset &= ~(bfd_vma) 0 << s->alignment_power;
10948 s->output_offset = offset / bfd_octets_per_byte (abfd);
10949 sections[n]->offset = offset;
10950 offset += sections[n]->size;
10958 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10962 if (flinfo->symstrtab != NULL)
10963 _bfd_elf_strtab_free (flinfo->symstrtab);
10964 if (flinfo->contents != NULL)
10965 free (flinfo->contents);
10966 if (flinfo->external_relocs != NULL)
10967 free (flinfo->external_relocs);
10968 if (flinfo->internal_relocs != NULL)
10969 free (flinfo->internal_relocs);
10970 if (flinfo->external_syms != NULL)
10971 free (flinfo->external_syms);
10972 if (flinfo->locsym_shndx != NULL)
10973 free (flinfo->locsym_shndx);
10974 if (flinfo->internal_syms != NULL)
10975 free (flinfo->internal_syms);
10976 if (flinfo->indices != NULL)
10977 free (flinfo->indices);
10978 if (flinfo->sections != NULL)
10979 free (flinfo->sections);
10980 if (flinfo->symshndxbuf != NULL)
10981 free (flinfo->symshndxbuf);
10982 for (o = obfd->sections; o != NULL; o = o->next)
10984 struct bfd_elf_section_data *esdo = elf_section_data (o);
10985 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10986 free (esdo->rel.hashes);
10987 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10988 free (esdo->rela.hashes);
10992 /* Do the final step of an ELF link. */
10995 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10997 bfd_boolean dynamic;
10998 bfd_boolean emit_relocs;
11000 struct elf_final_link_info flinfo;
11002 struct bfd_link_order *p;
11004 bfd_size_type max_contents_size;
11005 bfd_size_type max_external_reloc_size;
11006 bfd_size_type max_internal_reloc_count;
11007 bfd_size_type max_sym_count;
11008 bfd_size_type max_sym_shndx_count;
11009 Elf_Internal_Sym elfsym;
11011 Elf_Internal_Shdr *symtab_hdr;
11012 Elf_Internal_Shdr *symtab_shndx_hdr;
11013 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11014 struct elf_outext_info eoinfo;
11015 bfd_boolean merged;
11016 size_t relativecount = 0;
11017 asection *reldyn = 0;
11019 asection *attr_section = NULL;
11020 bfd_vma attr_size = 0;
11021 const char *std_attrs_section;
11023 if (! is_elf_hash_table (info->hash))
11026 if (bfd_link_pic (info))
11027 abfd->flags |= DYNAMIC;
11029 dynamic = elf_hash_table (info)->dynamic_sections_created;
11030 dynobj = elf_hash_table (info)->dynobj;
11032 emit_relocs = (bfd_link_relocatable (info)
11033 || info->emitrelocations);
11035 flinfo.info = info;
11036 flinfo.output_bfd = abfd;
11037 flinfo.symstrtab = _bfd_elf_strtab_init ();
11038 if (flinfo.symstrtab == NULL)
11043 flinfo.hash_sec = NULL;
11044 flinfo.symver_sec = NULL;
11048 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11049 /* Note that dynsym_sec can be NULL (on VMS). */
11050 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11051 /* Note that it is OK if symver_sec is NULL. */
11054 flinfo.contents = NULL;
11055 flinfo.external_relocs = NULL;
11056 flinfo.internal_relocs = NULL;
11057 flinfo.external_syms = NULL;
11058 flinfo.locsym_shndx = NULL;
11059 flinfo.internal_syms = NULL;
11060 flinfo.indices = NULL;
11061 flinfo.sections = NULL;
11062 flinfo.symshndxbuf = NULL;
11063 flinfo.filesym_count = 0;
11065 /* The object attributes have been merged. Remove the input
11066 sections from the link, and set the contents of the output
11068 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11069 for (o = abfd->sections; o != NULL; o = o->next)
11071 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11072 || strcmp (o->name, ".gnu.attributes") == 0)
11074 for (p = o->map_head.link_order; p != NULL; p = p->next)
11076 asection *input_section;
11078 if (p->type != bfd_indirect_link_order)
11080 input_section = p->u.indirect.section;
11081 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11082 elf_link_input_bfd ignores this section. */
11083 input_section->flags &= ~SEC_HAS_CONTENTS;
11086 attr_size = bfd_elf_obj_attr_size (abfd);
11089 bfd_set_section_size (abfd, o, attr_size);
11091 /* Skip this section later on. */
11092 o->map_head.link_order = NULL;
11095 o->flags |= SEC_EXCLUDE;
11099 /* Count up the number of relocations we will output for each output
11100 section, so that we know the sizes of the reloc sections. We
11101 also figure out some maximum sizes. */
11102 max_contents_size = 0;
11103 max_external_reloc_size = 0;
11104 max_internal_reloc_count = 0;
11106 max_sym_shndx_count = 0;
11108 for (o = abfd->sections; o != NULL; o = o->next)
11110 struct bfd_elf_section_data *esdo = elf_section_data (o);
11111 o->reloc_count = 0;
11113 for (p = o->map_head.link_order; p != NULL; p = p->next)
11115 unsigned int reloc_count = 0;
11116 unsigned int additional_reloc_count = 0;
11117 struct bfd_elf_section_data *esdi = NULL;
11119 if (p->type == bfd_section_reloc_link_order
11120 || p->type == bfd_symbol_reloc_link_order)
11122 else if (p->type == bfd_indirect_link_order)
11126 sec = p->u.indirect.section;
11127 esdi = elf_section_data (sec);
11129 /* Mark all sections which are to be included in the
11130 link. This will normally be every section. We need
11131 to do this so that we can identify any sections which
11132 the linker has decided to not include. */
11133 sec->linker_mark = TRUE;
11135 if (sec->flags & SEC_MERGE)
11138 if (esdo->this_hdr.sh_type == SHT_REL
11139 || esdo->this_hdr.sh_type == SHT_RELA)
11140 /* Some backends use reloc_count in relocation sections
11141 to count particular types of relocs. Of course,
11142 reloc sections themselves can't have relocations. */
11144 else if (emit_relocs)
11146 reloc_count = sec->reloc_count;
11147 if (bed->elf_backend_count_additional_relocs)
11150 c = (*bed->elf_backend_count_additional_relocs) (sec);
11151 additional_reloc_count += c;
11154 else if (bed->elf_backend_count_relocs)
11155 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11157 if (sec->rawsize > max_contents_size)
11158 max_contents_size = sec->rawsize;
11159 if (sec->size > max_contents_size)
11160 max_contents_size = sec->size;
11162 /* We are interested in just local symbols, not all
11164 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11165 && (sec->owner->flags & DYNAMIC) == 0)
11169 if (elf_bad_symtab (sec->owner))
11170 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11171 / bed->s->sizeof_sym);
11173 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11175 if (sym_count > max_sym_count)
11176 max_sym_count = sym_count;
11178 if (sym_count > max_sym_shndx_count
11179 && elf_symtab_shndx_list (sec->owner) != NULL)
11180 max_sym_shndx_count = sym_count;
11182 if ((sec->flags & SEC_RELOC) != 0)
11184 size_t ext_size = 0;
11186 if (esdi->rel.hdr != NULL)
11187 ext_size = esdi->rel.hdr->sh_size;
11188 if (esdi->rela.hdr != NULL)
11189 ext_size += esdi->rela.hdr->sh_size;
11191 if (ext_size > max_external_reloc_size)
11192 max_external_reloc_size = ext_size;
11193 if (sec->reloc_count > max_internal_reloc_count)
11194 max_internal_reloc_count = sec->reloc_count;
11199 if (reloc_count == 0)
11202 reloc_count += additional_reloc_count;
11203 o->reloc_count += reloc_count;
11205 if (p->type == bfd_indirect_link_order && emit_relocs)
11209 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11210 esdo->rel.count += additional_reloc_count;
11212 if (esdi->rela.hdr)
11214 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11215 esdo->rela.count += additional_reloc_count;
11221 esdo->rela.count += reloc_count;
11223 esdo->rel.count += reloc_count;
11227 if (o->reloc_count > 0)
11228 o->flags |= SEC_RELOC;
11231 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11232 set it (this is probably a bug) and if it is set
11233 assign_section_numbers will create a reloc section. */
11234 o->flags &=~ SEC_RELOC;
11237 /* If the SEC_ALLOC flag is not set, force the section VMA to
11238 zero. This is done in elf_fake_sections as well, but forcing
11239 the VMA to 0 here will ensure that relocs against these
11240 sections are handled correctly. */
11241 if ((o->flags & SEC_ALLOC) == 0
11242 && ! o->user_set_vma)
11246 if (! bfd_link_relocatable (info) && merged)
11247 elf_link_hash_traverse (elf_hash_table (info),
11248 _bfd_elf_link_sec_merge_syms, abfd);
11250 /* Figure out the file positions for everything but the symbol table
11251 and the relocs. We set symcount to force assign_section_numbers
11252 to create a symbol table. */
11253 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11254 BFD_ASSERT (! abfd->output_has_begun);
11255 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11258 /* Set sizes, and assign file positions for reloc sections. */
11259 for (o = abfd->sections; o != NULL; o = o->next)
11261 struct bfd_elf_section_data *esdo = elf_section_data (o);
11262 if ((o->flags & SEC_RELOC) != 0)
11265 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11269 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11273 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11274 to count upwards while actually outputting the relocations. */
11275 esdo->rel.count = 0;
11276 esdo->rela.count = 0;
11278 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11280 /* Cache the section contents so that they can be compressed
11281 later. Use bfd_malloc since it will be freed by
11282 bfd_compress_section_contents. */
11283 unsigned char *contents = esdo->this_hdr.contents;
11284 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11287 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11288 if (contents == NULL)
11290 esdo->this_hdr.contents = contents;
11294 /* We have now assigned file positions for all the sections except
11295 .symtab, .strtab, and non-loaded reloc sections. We start the
11296 .symtab section at the current file position, and write directly
11297 to it. We build the .strtab section in memory. */
11298 bfd_get_symcount (abfd) = 0;
11299 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11300 /* sh_name is set in prep_headers. */
11301 symtab_hdr->sh_type = SHT_SYMTAB;
11302 /* sh_flags, sh_addr and sh_size all start off zero. */
11303 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11304 /* sh_link is set in assign_section_numbers. */
11305 /* sh_info is set below. */
11306 /* sh_offset is set just below. */
11307 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11309 if (max_sym_count < 20)
11310 max_sym_count = 20;
11311 elf_hash_table (info)->strtabsize = max_sym_count;
11312 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11313 elf_hash_table (info)->strtab
11314 = (struct elf_sym_strtab *) bfd_malloc (amt);
11315 if (elf_hash_table (info)->strtab == NULL)
11317 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11319 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11320 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11322 if (info->strip != strip_all || emit_relocs)
11324 file_ptr off = elf_next_file_pos (abfd);
11326 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11328 /* Note that at this point elf_next_file_pos (abfd) is
11329 incorrect. We do not yet know the size of the .symtab section.
11330 We correct next_file_pos below, after we do know the size. */
11332 /* Start writing out the symbol table. The first symbol is always a
11334 elfsym.st_value = 0;
11335 elfsym.st_size = 0;
11336 elfsym.st_info = 0;
11337 elfsym.st_other = 0;
11338 elfsym.st_shndx = SHN_UNDEF;
11339 elfsym.st_target_internal = 0;
11340 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11341 bfd_und_section_ptr, NULL) != 1)
11344 /* Output a symbol for each section. We output these even if we are
11345 discarding local symbols, since they are used for relocs. These
11346 symbols have no names. We store the index of each one in the
11347 index field of the section, so that we can find it again when
11348 outputting relocs. */
11350 elfsym.st_size = 0;
11351 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11352 elfsym.st_other = 0;
11353 elfsym.st_value = 0;
11354 elfsym.st_target_internal = 0;
11355 for (i = 1; i < elf_numsections (abfd); i++)
11357 o = bfd_section_from_elf_index (abfd, i);
11360 o->target_index = bfd_get_symcount (abfd);
11361 elfsym.st_shndx = i;
11362 if (!bfd_link_relocatable (info))
11363 elfsym.st_value = o->vma;
11364 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11371 /* Allocate some memory to hold information read in from the input
11373 if (max_contents_size != 0)
11375 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11376 if (flinfo.contents == NULL)
11380 if (max_external_reloc_size != 0)
11382 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11383 if (flinfo.external_relocs == NULL)
11387 if (max_internal_reloc_count != 0)
11389 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11390 amt *= sizeof (Elf_Internal_Rela);
11391 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11392 if (flinfo.internal_relocs == NULL)
11396 if (max_sym_count != 0)
11398 amt = max_sym_count * bed->s->sizeof_sym;
11399 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11400 if (flinfo.external_syms == NULL)
11403 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11404 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11405 if (flinfo.internal_syms == NULL)
11408 amt = max_sym_count * sizeof (long);
11409 flinfo.indices = (long int *) bfd_malloc (amt);
11410 if (flinfo.indices == NULL)
11413 amt = max_sym_count * sizeof (asection *);
11414 flinfo.sections = (asection **) bfd_malloc (amt);
11415 if (flinfo.sections == NULL)
11419 if (max_sym_shndx_count != 0)
11421 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11422 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11423 if (flinfo.locsym_shndx == NULL)
11427 if (elf_hash_table (info)->tls_sec)
11429 bfd_vma base, end = 0;
11432 for (sec = elf_hash_table (info)->tls_sec;
11433 sec && (sec->flags & SEC_THREAD_LOCAL);
11436 bfd_size_type size = sec->size;
11439 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11441 struct bfd_link_order *ord = sec->map_tail.link_order;
11444 size = ord->offset + ord->size;
11446 end = sec->vma + size;
11448 base = elf_hash_table (info)->tls_sec->vma;
11449 /* Only align end of TLS section if static TLS doesn't have special
11450 alignment requirements. */
11451 if (bed->static_tls_alignment == 1)
11452 end = align_power (end,
11453 elf_hash_table (info)->tls_sec->alignment_power);
11454 elf_hash_table (info)->tls_size = end - base;
11457 /* Reorder SHF_LINK_ORDER sections. */
11458 for (o = abfd->sections; o != NULL; o = o->next)
11460 if (!elf_fixup_link_order (abfd, o))
11464 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11467 /* Since ELF permits relocations to be against local symbols, we
11468 must have the local symbols available when we do the relocations.
11469 Since we would rather only read the local symbols once, and we
11470 would rather not keep them in memory, we handle all the
11471 relocations for a single input file at the same time.
11473 Unfortunately, there is no way to know the total number of local
11474 symbols until we have seen all of them, and the local symbol
11475 indices precede the global symbol indices. This means that when
11476 we are generating relocatable output, and we see a reloc against
11477 a global symbol, we can not know the symbol index until we have
11478 finished examining all the local symbols to see which ones we are
11479 going to output. To deal with this, we keep the relocations in
11480 memory, and don't output them until the end of the link. This is
11481 an unfortunate waste of memory, but I don't see a good way around
11482 it. Fortunately, it only happens when performing a relocatable
11483 link, which is not the common case. FIXME: If keep_memory is set
11484 we could write the relocs out and then read them again; I don't
11485 know how bad the memory loss will be. */
11487 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11488 sub->output_has_begun = FALSE;
11489 for (o = abfd->sections; o != NULL; o = o->next)
11491 for (p = o->map_head.link_order; p != NULL; p = p->next)
11493 if (p->type == bfd_indirect_link_order
11494 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11495 == bfd_target_elf_flavour)
11496 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11498 if (! sub->output_has_begun)
11500 if (! elf_link_input_bfd (&flinfo, sub))
11502 sub->output_has_begun = TRUE;
11505 else if (p->type == bfd_section_reloc_link_order
11506 || p->type == bfd_symbol_reloc_link_order)
11508 if (! elf_reloc_link_order (abfd, info, o, p))
11513 if (! _bfd_default_link_order (abfd, info, o, p))
11515 if (p->type == bfd_indirect_link_order
11516 && (bfd_get_flavour (sub)
11517 == bfd_target_elf_flavour)
11518 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11519 != bed->s->elfclass))
11521 const char *iclass, *oclass;
11523 switch (bed->s->elfclass)
11525 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11526 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11527 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11531 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11533 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11534 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11535 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11539 bfd_set_error (bfd_error_wrong_format);
11540 (*_bfd_error_handler)
11541 (_("%B: file class %s incompatible with %s"),
11542 sub, iclass, oclass);
11551 /* Free symbol buffer if needed. */
11552 if (!info->reduce_memory_overheads)
11554 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11555 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11556 && elf_tdata (sub)->symbuf)
11558 free (elf_tdata (sub)->symbuf);
11559 elf_tdata (sub)->symbuf = NULL;
11563 /* Output any global symbols that got converted to local in a
11564 version script or due to symbol visibility. We do this in a
11565 separate step since ELF requires all local symbols to appear
11566 prior to any global symbols. FIXME: We should only do this if
11567 some global symbols were, in fact, converted to become local.
11568 FIXME: Will this work correctly with the Irix 5 linker? */
11569 eoinfo.failed = FALSE;
11570 eoinfo.flinfo = &flinfo;
11571 eoinfo.localsyms = TRUE;
11572 eoinfo.file_sym_done = FALSE;
11573 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11577 /* If backend needs to output some local symbols not present in the hash
11578 table, do it now. */
11579 if (bed->elf_backend_output_arch_local_syms
11580 && (info->strip != strip_all || emit_relocs))
11582 typedef int (*out_sym_func)
11583 (void *, const char *, Elf_Internal_Sym *, asection *,
11584 struct elf_link_hash_entry *);
11586 if (! ((*bed->elf_backend_output_arch_local_syms)
11587 (abfd, info, &flinfo,
11588 (out_sym_func) elf_link_output_symstrtab)))
11592 /* That wrote out all the local symbols. Finish up the symbol table
11593 with the global symbols. Even if we want to strip everything we
11594 can, we still need to deal with those global symbols that got
11595 converted to local in a version script. */
11597 /* The sh_info field records the index of the first non local symbol. */
11598 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11601 && elf_hash_table (info)->dynsym != NULL
11602 && (elf_hash_table (info)->dynsym->output_section
11603 != bfd_abs_section_ptr))
11605 Elf_Internal_Sym sym;
11606 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11607 long last_local = 0;
11609 /* Write out the section symbols for the output sections. */
11610 if (bfd_link_pic (info)
11611 || elf_hash_table (info)->is_relocatable_executable)
11617 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11619 sym.st_target_internal = 0;
11621 for (s = abfd->sections; s != NULL; s = s->next)
11627 dynindx = elf_section_data (s)->dynindx;
11630 indx = elf_section_data (s)->this_idx;
11631 BFD_ASSERT (indx > 0);
11632 sym.st_shndx = indx;
11633 if (! check_dynsym (abfd, &sym))
11635 sym.st_value = s->vma;
11636 dest = dynsym + dynindx * bed->s->sizeof_sym;
11637 if (last_local < dynindx)
11638 last_local = dynindx;
11639 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11643 /* Write out the local dynsyms. */
11644 if (elf_hash_table (info)->dynlocal)
11646 struct elf_link_local_dynamic_entry *e;
11647 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11652 /* Copy the internal symbol and turn off visibility.
11653 Note that we saved a word of storage and overwrote
11654 the original st_name with the dynstr_index. */
11656 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11658 s = bfd_section_from_elf_index (e->input_bfd,
11663 elf_section_data (s->output_section)->this_idx;
11664 if (! check_dynsym (abfd, &sym))
11666 sym.st_value = (s->output_section->vma
11668 + e->isym.st_value);
11671 if (last_local < e->dynindx)
11672 last_local = e->dynindx;
11674 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11675 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11679 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11683 /* We get the global symbols from the hash table. */
11684 eoinfo.failed = FALSE;
11685 eoinfo.localsyms = FALSE;
11686 eoinfo.flinfo = &flinfo;
11687 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11691 /* If backend needs to output some symbols not present in the hash
11692 table, do it now. */
11693 if (bed->elf_backend_output_arch_syms
11694 && (info->strip != strip_all || emit_relocs))
11696 typedef int (*out_sym_func)
11697 (void *, const char *, Elf_Internal_Sym *, asection *,
11698 struct elf_link_hash_entry *);
11700 if (! ((*bed->elf_backend_output_arch_syms)
11701 (abfd, info, &flinfo,
11702 (out_sym_func) elf_link_output_symstrtab)))
11706 /* Finalize the .strtab section. */
11707 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11709 /* Swap out the .strtab section. */
11710 if (!elf_link_swap_symbols_out (&flinfo))
11713 /* Now we know the size of the symtab section. */
11714 if (bfd_get_symcount (abfd) > 0)
11716 /* Finish up and write out the symbol string table (.strtab)
11718 Elf_Internal_Shdr *symstrtab_hdr;
11719 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11721 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11722 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11724 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11725 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11726 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11727 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11728 symtab_shndx_hdr->sh_size = amt;
11730 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11733 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11734 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11738 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11739 /* sh_name was set in prep_headers. */
11740 symstrtab_hdr->sh_type = SHT_STRTAB;
11741 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
11742 symstrtab_hdr->sh_addr = 0;
11743 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11744 symstrtab_hdr->sh_entsize = 0;
11745 symstrtab_hdr->sh_link = 0;
11746 symstrtab_hdr->sh_info = 0;
11747 /* sh_offset is set just below. */
11748 symstrtab_hdr->sh_addralign = 1;
11750 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11752 elf_next_file_pos (abfd) = off;
11754 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11755 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11759 /* Adjust the relocs to have the correct symbol indices. */
11760 for (o = abfd->sections; o != NULL; o = o->next)
11762 struct bfd_elf_section_data *esdo = elf_section_data (o);
11764 if ((o->flags & SEC_RELOC) == 0)
11767 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11768 if (esdo->rel.hdr != NULL
11769 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11771 if (esdo->rela.hdr != NULL
11772 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11775 /* Set the reloc_count field to 0 to prevent write_relocs from
11776 trying to swap the relocs out itself. */
11777 o->reloc_count = 0;
11780 if (dynamic && info->combreloc && dynobj != NULL)
11781 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11783 /* If we are linking against a dynamic object, or generating a
11784 shared library, finish up the dynamic linking information. */
11787 bfd_byte *dyncon, *dynconend;
11789 /* Fix up .dynamic entries. */
11790 o = bfd_get_linker_section (dynobj, ".dynamic");
11791 BFD_ASSERT (o != NULL);
11793 dyncon = o->contents;
11794 dynconend = o->contents + o->size;
11795 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11797 Elf_Internal_Dyn dyn;
11801 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11808 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11810 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11812 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11813 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11816 dyn.d_un.d_val = relativecount;
11823 name = info->init_function;
11826 name = info->fini_function;
11829 struct elf_link_hash_entry *h;
11831 h = elf_link_hash_lookup (elf_hash_table (info), name,
11832 FALSE, FALSE, TRUE);
11834 && (h->root.type == bfd_link_hash_defined
11835 || h->root.type == bfd_link_hash_defweak))
11837 dyn.d_un.d_ptr = h->root.u.def.value;
11838 o = h->root.u.def.section;
11839 if (o->output_section != NULL)
11840 dyn.d_un.d_ptr += (o->output_section->vma
11841 + o->output_offset);
11844 /* The symbol is imported from another shared
11845 library and does not apply to this one. */
11846 dyn.d_un.d_ptr = 0;
11853 case DT_PREINIT_ARRAYSZ:
11854 name = ".preinit_array";
11856 case DT_INIT_ARRAYSZ:
11857 name = ".init_array";
11859 case DT_FINI_ARRAYSZ:
11860 name = ".fini_array";
11862 o = bfd_get_section_by_name (abfd, name);
11865 (*_bfd_error_handler)
11866 (_("%B: could not find output section %s"), abfd, name);
11870 (*_bfd_error_handler)
11871 (_("warning: %s section has zero size"), name);
11872 dyn.d_un.d_val = o->size;
11875 case DT_PREINIT_ARRAY:
11876 name = ".preinit_array";
11878 case DT_INIT_ARRAY:
11879 name = ".init_array";
11881 case DT_FINI_ARRAY:
11882 name = ".fini_array";
11889 name = ".gnu.hash";
11898 name = ".gnu.version_d";
11901 name = ".gnu.version_r";
11904 name = ".gnu.version";
11906 o = bfd_get_section_by_name (abfd, name);
11909 (*_bfd_error_handler)
11910 (_("%B: could not find output section %s"), abfd, name);
11913 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11915 (*_bfd_error_handler)
11916 (_("warning: section '%s' is being made into a note"), name);
11917 bfd_set_error (bfd_error_nonrepresentable_section);
11920 dyn.d_un.d_ptr = o->vma;
11927 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11931 dyn.d_un.d_val = 0;
11932 dyn.d_un.d_ptr = 0;
11933 for (i = 1; i < elf_numsections (abfd); i++)
11935 Elf_Internal_Shdr *hdr;
11937 hdr = elf_elfsections (abfd)[i];
11938 if (hdr->sh_type == type
11939 && (hdr->sh_flags & SHF_ALLOC) != 0)
11941 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11942 dyn.d_un.d_val += hdr->sh_size;
11945 if (dyn.d_un.d_ptr == 0
11946 || hdr->sh_addr < dyn.d_un.d_ptr)
11947 dyn.d_un.d_ptr = hdr->sh_addr;
11953 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11957 /* If we have created any dynamic sections, then output them. */
11958 if (dynobj != NULL)
11960 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11963 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11964 if (((info->warn_shared_textrel && bfd_link_pic (info))
11965 || info->error_textrel)
11966 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11968 bfd_byte *dyncon, *dynconend;
11970 dyncon = o->contents;
11971 dynconend = o->contents + o->size;
11972 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11974 Elf_Internal_Dyn dyn;
11976 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11978 if (dyn.d_tag == DT_TEXTREL)
11980 if (info->error_textrel)
11981 info->callbacks->einfo
11982 (_("%P%X: read-only segment has dynamic relocations.\n"));
11984 info->callbacks->einfo
11985 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11991 for (o = dynobj->sections; o != NULL; o = o->next)
11993 if ((o->flags & SEC_HAS_CONTENTS) == 0
11995 || o->output_section == bfd_abs_section_ptr)
11997 if ((o->flags & SEC_LINKER_CREATED) == 0)
11999 /* At this point, we are only interested in sections
12000 created by _bfd_elf_link_create_dynamic_sections. */
12003 if (elf_hash_table (info)->stab_info.stabstr == o)
12005 if (elf_hash_table (info)->eh_info.hdr_sec == o)
12007 if (strcmp (o->name, ".dynstr") != 0)
12009 if (! bfd_set_section_contents (abfd, o->output_section,
12011 (file_ptr) o->output_offset
12012 * bfd_octets_per_byte (abfd),
12018 /* The contents of the .dynstr section are actually in a
12022 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12023 if (bfd_seek (abfd, off, SEEK_SET) != 0
12024 || ! _bfd_elf_strtab_emit (abfd,
12025 elf_hash_table (info)->dynstr))
12031 if (bfd_link_relocatable (info))
12033 bfd_boolean failed = FALSE;
12035 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12040 /* If we have optimized stabs strings, output them. */
12041 if (elf_hash_table (info)->stab_info.stabstr != NULL)
12043 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12047 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12050 elf_final_link_free (abfd, &flinfo);
12052 elf_linker (abfd) = TRUE;
12056 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12057 if (contents == NULL)
12058 return FALSE; /* Bail out and fail. */
12059 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12060 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12067 elf_final_link_free (abfd, &flinfo);
12071 /* Initialize COOKIE for input bfd ABFD. */
12074 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12075 struct bfd_link_info *info, bfd *abfd)
12077 Elf_Internal_Shdr *symtab_hdr;
12078 const struct elf_backend_data *bed;
12080 bed = get_elf_backend_data (abfd);
12081 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12083 cookie->abfd = abfd;
12084 cookie->sym_hashes = elf_sym_hashes (abfd);
12085 cookie->bad_symtab = elf_bad_symtab (abfd);
12086 if (cookie->bad_symtab)
12088 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12089 cookie->extsymoff = 0;
12093 cookie->locsymcount = symtab_hdr->sh_info;
12094 cookie->extsymoff = symtab_hdr->sh_info;
12097 if (bed->s->arch_size == 32)
12098 cookie->r_sym_shift = 8;
12100 cookie->r_sym_shift = 32;
12102 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12103 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12105 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12106 cookie->locsymcount, 0,
12108 if (cookie->locsyms == NULL)
12110 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12113 if (info->keep_memory)
12114 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12119 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12122 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12124 Elf_Internal_Shdr *symtab_hdr;
12126 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12127 if (cookie->locsyms != NULL
12128 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12129 free (cookie->locsyms);
12132 /* Initialize the relocation information in COOKIE for input section SEC
12133 of input bfd ABFD. */
12136 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12137 struct bfd_link_info *info, bfd *abfd,
12140 const struct elf_backend_data *bed;
12142 if (sec->reloc_count == 0)
12144 cookie->rels = NULL;
12145 cookie->relend = NULL;
12149 bed = get_elf_backend_data (abfd);
12151 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12152 info->keep_memory);
12153 if (cookie->rels == NULL)
12155 cookie->rel = cookie->rels;
12156 cookie->relend = (cookie->rels
12157 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12159 cookie->rel = cookie->rels;
12163 /* Free the memory allocated by init_reloc_cookie_rels,
12167 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12170 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12171 free (cookie->rels);
12174 /* Initialize the whole of COOKIE for input section SEC. */
12177 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12178 struct bfd_link_info *info,
12181 if (!init_reloc_cookie (cookie, info, sec->owner))
12183 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12188 fini_reloc_cookie (cookie, sec->owner);
12193 /* Free the memory allocated by init_reloc_cookie_for_section,
12197 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12200 fini_reloc_cookie_rels (cookie, sec);
12201 fini_reloc_cookie (cookie, sec->owner);
12204 /* Garbage collect unused sections. */
12206 /* Default gc_mark_hook. */
12209 _bfd_elf_gc_mark_hook (asection *sec,
12210 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12211 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12212 struct elf_link_hash_entry *h,
12213 Elf_Internal_Sym *sym)
12217 switch (h->root.type)
12219 case bfd_link_hash_defined:
12220 case bfd_link_hash_defweak:
12221 return h->root.u.def.section;
12223 case bfd_link_hash_common:
12224 return h->root.u.c.p->section;
12231 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12236 /* COOKIE->rel describes a relocation against section SEC, which is
12237 a section we've decided to keep. Return the section that contains
12238 the relocation symbol, or NULL if no section contains it. */
12241 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12242 elf_gc_mark_hook_fn gc_mark_hook,
12243 struct elf_reloc_cookie *cookie,
12244 bfd_boolean *start_stop)
12246 unsigned long r_symndx;
12247 struct elf_link_hash_entry *h;
12249 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12250 if (r_symndx == STN_UNDEF)
12253 if (r_symndx >= cookie->locsymcount
12254 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12256 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12259 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12263 while (h->root.type == bfd_link_hash_indirect
12264 || h->root.type == bfd_link_hash_warning)
12265 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12267 /* If this symbol is weak and there is a non-weak definition, we
12268 keep the non-weak definition because many backends put
12269 dynamic reloc info on the non-weak definition for code
12270 handling copy relocs. */
12271 if (h->u.weakdef != NULL)
12272 h->u.weakdef->mark = 1;
12274 if (start_stop != NULL
12275 && (h->root.type == bfd_link_hash_undefined
12276 || h->root.type == bfd_link_hash_undefweak))
12278 /* To work around a glibc bug, mark all XXX input sections
12279 when there is an as yet undefined reference to __start_XXX
12280 or __stop_XXX symbols. The linker will later define such
12281 symbols for orphan input sections that have a name
12282 representable as a C identifier. */
12283 const char *sec_name = NULL;
12284 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12285 sec_name = h->root.root.string + 8;
12286 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12287 sec_name = h->root.root.string + 7;
12289 if (sec_name != NULL && *sec_name != '\0')
12293 for (i = info->input_bfds; i != NULL; i = i->link.next)
12295 asection *s = bfd_get_section_by_name (i, sec_name);
12296 if (s != NULL && !s->gc_mark)
12298 *start_stop = TRUE;
12305 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12308 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12309 &cookie->locsyms[r_symndx]);
12312 /* COOKIE->rel describes a relocation against section SEC, which is
12313 a section we've decided to keep. Mark the section that contains
12314 the relocation symbol. */
12317 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12319 elf_gc_mark_hook_fn gc_mark_hook,
12320 struct elf_reloc_cookie *cookie)
12323 bfd_boolean start_stop = FALSE;
12325 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12326 while (rsec != NULL)
12328 if (!rsec->gc_mark)
12330 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12331 || (rsec->owner->flags & DYNAMIC) != 0)
12333 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12338 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12343 /* The mark phase of garbage collection. For a given section, mark
12344 it and any sections in this section's group, and all the sections
12345 which define symbols to which it refers. */
12348 _bfd_elf_gc_mark (struct bfd_link_info *info,
12350 elf_gc_mark_hook_fn gc_mark_hook)
12353 asection *group_sec, *eh_frame;
12357 /* Mark all the sections in the group. */
12358 group_sec = elf_section_data (sec)->next_in_group;
12359 if (group_sec && !group_sec->gc_mark)
12360 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12363 /* Look through the section relocs. */
12365 eh_frame = elf_eh_frame_section (sec->owner);
12366 if ((sec->flags & SEC_RELOC) != 0
12367 && sec->reloc_count > 0
12368 && sec != eh_frame)
12370 struct elf_reloc_cookie cookie;
12372 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12376 for (; cookie.rel < cookie.relend; cookie.rel++)
12377 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12382 fini_reloc_cookie_for_section (&cookie, sec);
12386 if (ret && eh_frame && elf_fde_list (sec))
12388 struct elf_reloc_cookie cookie;
12390 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12394 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12395 gc_mark_hook, &cookie))
12397 fini_reloc_cookie_for_section (&cookie, eh_frame);
12401 eh_frame = elf_section_eh_frame_entry (sec);
12402 if (ret && eh_frame && !eh_frame->gc_mark)
12403 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12409 /* Scan and mark sections in a special or debug section group. */
12412 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12414 /* Point to first section of section group. */
12416 /* Used to iterate the section group. */
12419 bfd_boolean is_special_grp = TRUE;
12420 bfd_boolean is_debug_grp = TRUE;
12422 /* First scan to see if group contains any section other than debug
12423 and special section. */
12424 ssec = msec = elf_next_in_group (grp);
12427 if ((msec->flags & SEC_DEBUGGING) == 0)
12428 is_debug_grp = FALSE;
12430 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12431 is_special_grp = FALSE;
12433 msec = elf_next_in_group (msec);
12435 while (msec != ssec);
12437 /* If this is a pure debug section group or pure special section group,
12438 keep all sections in this group. */
12439 if (is_debug_grp || is_special_grp)
12444 msec = elf_next_in_group (msec);
12446 while (msec != ssec);
12450 /* Keep debug and special sections. */
12453 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12454 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12458 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12461 bfd_boolean some_kept;
12462 bfd_boolean debug_frag_seen;
12464 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12467 /* Ensure all linker created sections are kept,
12468 see if any other section is already marked,
12469 and note if we have any fragmented debug sections. */
12470 debug_frag_seen = some_kept = FALSE;
12471 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12473 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12475 else if (isec->gc_mark)
12478 if (debug_frag_seen == FALSE
12479 && (isec->flags & SEC_DEBUGGING)
12480 && CONST_STRNEQ (isec->name, ".debug_line."))
12481 debug_frag_seen = TRUE;
12484 /* If no section in this file will be kept, then we can
12485 toss out the debug and special sections. */
12489 /* Keep debug and special sections like .comment when they are
12490 not part of a group. Also keep section groups that contain
12491 just debug sections or special sections. */
12492 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12494 if ((isec->flags & SEC_GROUP) != 0)
12495 _bfd_elf_gc_mark_debug_special_section_group (isec);
12496 else if (((isec->flags & SEC_DEBUGGING) != 0
12497 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12498 && elf_next_in_group (isec) == NULL)
12502 if (! debug_frag_seen)
12505 /* Look for CODE sections which are going to be discarded,
12506 and find and discard any fragmented debug sections which
12507 are associated with that code section. */
12508 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12509 if ((isec->flags & SEC_CODE) != 0
12510 && isec->gc_mark == 0)
12515 ilen = strlen (isec->name);
12517 /* Association is determined by the name of the debug section
12518 containing the name of the code section as a suffix. For
12519 example .debug_line.text.foo is a debug section associated
12521 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12525 if (dsec->gc_mark == 0
12526 || (dsec->flags & SEC_DEBUGGING) == 0)
12529 dlen = strlen (dsec->name);
12532 && strncmp (dsec->name + (dlen - ilen),
12533 isec->name, ilen) == 0)
12543 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12545 struct elf_gc_sweep_symbol_info
12547 struct bfd_link_info *info;
12548 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12553 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12556 && (((h->root.type == bfd_link_hash_defined
12557 || h->root.type == bfd_link_hash_defweak)
12558 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12559 && h->root.u.def.section->gc_mark))
12560 || h->root.type == bfd_link_hash_undefined
12561 || h->root.type == bfd_link_hash_undefweak))
12563 struct elf_gc_sweep_symbol_info *inf;
12565 inf = (struct elf_gc_sweep_symbol_info *) data;
12566 (*inf->hide_symbol) (inf->info, h, TRUE);
12567 h->def_regular = 0;
12568 h->ref_regular = 0;
12569 h->ref_regular_nonweak = 0;
12575 /* The sweep phase of garbage collection. Remove all garbage sections. */
12577 typedef bfd_boolean (*gc_sweep_hook_fn)
12578 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12581 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12584 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12585 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12586 unsigned long section_sym_count;
12587 struct elf_gc_sweep_symbol_info sweep_info;
12589 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12593 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12594 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12597 for (o = sub->sections; o != NULL; o = o->next)
12599 /* When any section in a section group is kept, we keep all
12600 sections in the section group. If the first member of
12601 the section group is excluded, we will also exclude the
12603 if (o->flags & SEC_GROUP)
12605 asection *first = elf_next_in_group (o);
12606 o->gc_mark = first->gc_mark;
12612 /* Skip sweeping sections already excluded. */
12613 if (o->flags & SEC_EXCLUDE)
12616 /* Since this is early in the link process, it is simple
12617 to remove a section from the output. */
12618 o->flags |= SEC_EXCLUDE;
12620 if (info->print_gc_sections && o->size != 0)
12621 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12623 /* But we also have to update some of the relocation
12624 info we collected before. */
12626 && (o->flags & SEC_RELOC) != 0
12627 && o->reloc_count != 0
12628 && !((info->strip == strip_all || info->strip == strip_debugger)
12629 && (o->flags & SEC_DEBUGGING) != 0)
12630 && !bfd_is_abs_section (o->output_section))
12632 Elf_Internal_Rela *internal_relocs;
12636 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12637 info->keep_memory);
12638 if (internal_relocs == NULL)
12641 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12643 if (elf_section_data (o)->relocs != internal_relocs)
12644 free (internal_relocs);
12652 /* Remove the symbols that were in the swept sections from the dynamic
12653 symbol table. GCFIXME: Anyone know how to get them out of the
12654 static symbol table as well? */
12655 sweep_info.info = info;
12656 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12657 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12660 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
12664 /* Propagate collected vtable information. This is called through
12665 elf_link_hash_traverse. */
12668 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12670 /* Those that are not vtables. */
12671 if (h->vtable == NULL || h->vtable->parent == NULL)
12674 /* Those vtables that do not have parents, we cannot merge. */
12675 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12678 /* If we've already been done, exit. */
12679 if (h->vtable->used && h->vtable->used[-1])
12682 /* Make sure the parent's table is up to date. */
12683 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12685 if (h->vtable->used == NULL)
12687 /* None of this table's entries were referenced. Re-use the
12689 h->vtable->used = h->vtable->parent->vtable->used;
12690 h->vtable->size = h->vtable->parent->vtable->size;
12695 bfd_boolean *cu, *pu;
12697 /* Or the parent's entries into ours. */
12698 cu = h->vtable->used;
12700 pu = h->vtable->parent->vtable->used;
12703 const struct elf_backend_data *bed;
12704 unsigned int log_file_align;
12706 bed = get_elf_backend_data (h->root.u.def.section->owner);
12707 log_file_align = bed->s->log_file_align;
12708 n = h->vtable->parent->vtable->size >> log_file_align;
12723 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12726 bfd_vma hstart, hend;
12727 Elf_Internal_Rela *relstart, *relend, *rel;
12728 const struct elf_backend_data *bed;
12729 unsigned int log_file_align;
12731 /* Take care of both those symbols that do not describe vtables as
12732 well as those that are not loaded. */
12733 if (h->vtable == NULL || h->vtable->parent == NULL)
12736 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12737 || h->root.type == bfd_link_hash_defweak);
12739 sec = h->root.u.def.section;
12740 hstart = h->root.u.def.value;
12741 hend = hstart + h->size;
12743 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12745 return *(bfd_boolean *) okp = FALSE;
12746 bed = get_elf_backend_data (sec->owner);
12747 log_file_align = bed->s->log_file_align;
12749 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12751 for (rel = relstart; rel < relend; ++rel)
12752 if (rel->r_offset >= hstart && rel->r_offset < hend)
12754 /* If the entry is in use, do nothing. */
12755 if (h->vtable->used
12756 && (rel->r_offset - hstart) < h->vtable->size)
12758 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12759 if (h->vtable->used[entry])
12762 /* Otherwise, kill it. */
12763 rel->r_offset = rel->r_info = rel->r_addend = 0;
12769 /* Mark sections containing dynamically referenced symbols. When
12770 building shared libraries, we must assume that any visible symbol is
12774 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12776 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12777 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12779 if ((h->root.type == bfd_link_hash_defined
12780 || h->root.type == bfd_link_hash_defweak)
12782 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12783 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12784 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12785 && (!bfd_link_executable (info)
12786 || info->export_dynamic
12789 && (*d->match) (&d->head, NULL, h->root.root.string)))
12790 && (h->versioned >= versioned
12791 || !bfd_hide_sym_by_version (info->version_info,
12792 h->root.root.string)))))
12793 h->root.u.def.section->flags |= SEC_KEEP;
12798 /* Keep all sections containing symbols undefined on the command-line,
12799 and the section containing the entry symbol. */
12802 _bfd_elf_gc_keep (struct bfd_link_info *info)
12804 struct bfd_sym_chain *sym;
12806 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12808 struct elf_link_hash_entry *h;
12810 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12811 FALSE, FALSE, FALSE);
12814 && (h->root.type == bfd_link_hash_defined
12815 || h->root.type == bfd_link_hash_defweak)
12816 && !bfd_is_abs_section (h->root.u.def.section))
12817 h->root.u.def.section->flags |= SEC_KEEP;
12822 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12823 struct bfd_link_info *info)
12825 bfd *ibfd = info->input_bfds;
12827 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12830 struct elf_reloc_cookie cookie;
12832 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12835 if (!init_reloc_cookie (&cookie, info, ibfd))
12838 for (sec = ibfd->sections; sec; sec = sec->next)
12840 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12841 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12843 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12844 fini_reloc_cookie_rels (&cookie, sec);
12851 /* Do mark and sweep of unused sections. */
12854 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12856 bfd_boolean ok = TRUE;
12858 elf_gc_mark_hook_fn gc_mark_hook;
12859 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12860 struct elf_link_hash_table *htab;
12862 if (!bed->can_gc_sections
12863 || !is_elf_hash_table (info->hash))
12865 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12869 bed->gc_keep (info);
12870 htab = elf_hash_table (info);
12872 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12873 at the .eh_frame section if we can mark the FDEs individually. */
12874 for (sub = info->input_bfds;
12875 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12876 sub = sub->link.next)
12879 struct elf_reloc_cookie cookie;
12881 sec = bfd_get_section_by_name (sub, ".eh_frame");
12882 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12884 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12885 if (elf_section_data (sec)->sec_info
12886 && (sec->flags & SEC_LINKER_CREATED) == 0)
12887 elf_eh_frame_section (sub) = sec;
12888 fini_reloc_cookie_for_section (&cookie, sec);
12889 sec = bfd_get_next_section_by_name (NULL, sec);
12893 /* Apply transitive closure to the vtable entry usage info. */
12894 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12898 /* Kill the vtable relocations that were not used. */
12899 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12903 /* Mark dynamically referenced symbols. */
12904 if (htab->dynamic_sections_created)
12905 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12907 /* Grovel through relocs to find out who stays ... */
12908 gc_mark_hook = bed->gc_mark_hook;
12909 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12913 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12914 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12917 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12918 Also treat note sections as a root, if the section is not part
12920 for (o = sub->sections; o != NULL; o = o->next)
12922 && (o->flags & SEC_EXCLUDE) == 0
12923 && ((o->flags & SEC_KEEP) != 0
12924 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12925 && elf_next_in_group (o) == NULL )))
12927 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12932 /* Allow the backend to mark additional target specific sections. */
12933 bed->gc_mark_extra_sections (info, gc_mark_hook);
12935 /* ... and mark SEC_EXCLUDE for those that go. */
12936 return elf_gc_sweep (abfd, info);
12939 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12942 bfd_elf_gc_record_vtinherit (bfd *abfd,
12944 struct elf_link_hash_entry *h,
12947 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12948 struct elf_link_hash_entry **search, *child;
12949 bfd_size_type extsymcount;
12950 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12952 /* The sh_info field of the symtab header tells us where the
12953 external symbols start. We don't care about the local symbols at
12955 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12956 if (!elf_bad_symtab (abfd))
12957 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12959 sym_hashes = elf_sym_hashes (abfd);
12960 sym_hashes_end = sym_hashes + extsymcount;
12962 /* Hunt down the child symbol, which is in this section at the same
12963 offset as the relocation. */
12964 for (search = sym_hashes; search != sym_hashes_end; ++search)
12966 if ((child = *search) != NULL
12967 && (child->root.type == bfd_link_hash_defined
12968 || child->root.type == bfd_link_hash_defweak)
12969 && child->root.u.def.section == sec
12970 && child->root.u.def.value == offset)
12974 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12975 abfd, sec, (unsigned long) offset);
12976 bfd_set_error (bfd_error_invalid_operation);
12980 if (!child->vtable)
12982 child->vtable = ((struct elf_link_virtual_table_entry *)
12983 bfd_zalloc (abfd, sizeof (*child->vtable)));
12984 if (!child->vtable)
12989 /* This *should* only be the absolute section. It could potentially
12990 be that someone has defined a non-global vtable though, which
12991 would be bad. It isn't worth paging in the local symbols to be
12992 sure though; that case should simply be handled by the assembler. */
12994 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12997 child->vtable->parent = h;
13002 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13005 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13006 asection *sec ATTRIBUTE_UNUSED,
13007 struct elf_link_hash_entry *h,
13010 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13011 unsigned int log_file_align = bed->s->log_file_align;
13015 h->vtable = ((struct elf_link_virtual_table_entry *)
13016 bfd_zalloc (abfd, sizeof (*h->vtable)));
13021 if (addend >= h->vtable->size)
13023 size_t size, bytes, file_align;
13024 bfd_boolean *ptr = h->vtable->used;
13026 /* While the symbol is undefined, we have to be prepared to handle
13028 file_align = 1 << log_file_align;
13029 if (h->root.type == bfd_link_hash_undefined)
13030 size = addend + file_align;
13034 if (addend >= size)
13036 /* Oops! We've got a reference past the defined end of
13037 the table. This is probably a bug -- shall we warn? */
13038 size = addend + file_align;
13041 size = (size + file_align - 1) & -file_align;
13043 /* Allocate one extra entry for use as a "done" flag for the
13044 consolidation pass. */
13045 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13049 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13055 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13056 * sizeof (bfd_boolean));
13057 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13061 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13066 /* And arrange for that done flag to be at index -1. */
13067 h->vtable->used = ptr + 1;
13068 h->vtable->size = size;
13071 h->vtable->used[addend >> log_file_align] = TRUE;
13076 /* Map an ELF section header flag to its corresponding string. */
13080 flagword flag_value;
13081 } elf_flags_to_name_table;
13083 static elf_flags_to_name_table elf_flags_to_names [] =
13085 { "SHF_WRITE", SHF_WRITE },
13086 { "SHF_ALLOC", SHF_ALLOC },
13087 { "SHF_EXECINSTR", SHF_EXECINSTR },
13088 { "SHF_MERGE", SHF_MERGE },
13089 { "SHF_STRINGS", SHF_STRINGS },
13090 { "SHF_INFO_LINK", SHF_INFO_LINK},
13091 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13092 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13093 { "SHF_GROUP", SHF_GROUP },
13094 { "SHF_TLS", SHF_TLS },
13095 { "SHF_MASKOS", SHF_MASKOS },
13096 { "SHF_EXCLUDE", SHF_EXCLUDE },
13099 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13101 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13102 struct flag_info *flaginfo,
13105 const bfd_vma sh_flags = elf_section_flags (section);
13107 if (!flaginfo->flags_initialized)
13109 bfd *obfd = info->output_bfd;
13110 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13111 struct flag_info_list *tf = flaginfo->flag_list;
13113 int without_hex = 0;
13115 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13118 flagword (*lookup) (char *);
13120 lookup = bed->elf_backend_lookup_section_flags_hook;
13121 if (lookup != NULL)
13123 flagword hexval = (*lookup) ((char *) tf->name);
13127 if (tf->with == with_flags)
13128 with_hex |= hexval;
13129 else if (tf->with == without_flags)
13130 without_hex |= hexval;
13135 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13137 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13139 if (tf->with == with_flags)
13140 with_hex |= elf_flags_to_names[i].flag_value;
13141 else if (tf->with == without_flags)
13142 without_hex |= elf_flags_to_names[i].flag_value;
13149 info->callbacks->einfo
13150 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13154 flaginfo->flags_initialized = TRUE;
13155 flaginfo->only_with_flags |= with_hex;
13156 flaginfo->not_with_flags |= without_hex;
13159 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13162 if ((flaginfo->not_with_flags & sh_flags) != 0)
13168 struct alloc_got_off_arg {
13170 struct bfd_link_info *info;
13173 /* We need a special top-level link routine to convert got reference counts
13174 to real got offsets. */
13177 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13179 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13180 bfd *obfd = gofarg->info->output_bfd;
13181 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13183 if (h->got.refcount > 0)
13185 h->got.offset = gofarg->gotoff;
13186 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13189 h->got.offset = (bfd_vma) -1;
13194 /* And an accompanying bit to work out final got entry offsets once
13195 we're done. Should be called from final_link. */
13198 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13199 struct bfd_link_info *info)
13202 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13204 struct alloc_got_off_arg gofarg;
13206 BFD_ASSERT (abfd == info->output_bfd);
13208 if (! is_elf_hash_table (info->hash))
13211 /* The GOT offset is relative to the .got section, but the GOT header is
13212 put into the .got.plt section, if the backend uses it. */
13213 if (bed->want_got_plt)
13216 gotoff = bed->got_header_size;
13218 /* Do the local .got entries first. */
13219 for (i = info->input_bfds; i; i = i->link.next)
13221 bfd_signed_vma *local_got;
13222 bfd_size_type j, locsymcount;
13223 Elf_Internal_Shdr *symtab_hdr;
13225 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13228 local_got = elf_local_got_refcounts (i);
13232 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13233 if (elf_bad_symtab (i))
13234 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13236 locsymcount = symtab_hdr->sh_info;
13238 for (j = 0; j < locsymcount; ++j)
13240 if (local_got[j] > 0)
13242 local_got[j] = gotoff;
13243 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13246 local_got[j] = (bfd_vma) -1;
13250 /* Then the global .got entries. .plt refcounts are handled by
13251 adjust_dynamic_symbol */
13252 gofarg.gotoff = gotoff;
13253 gofarg.info = info;
13254 elf_link_hash_traverse (elf_hash_table (info),
13255 elf_gc_allocate_got_offsets,
13260 /* Many folk need no more in the way of final link than this, once
13261 got entry reference counting is enabled. */
13264 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13266 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13269 /* Invoke the regular ELF backend linker to do all the work. */
13270 return bfd_elf_final_link (abfd, info);
13274 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13276 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13278 if (rcookie->bad_symtab)
13279 rcookie->rel = rcookie->rels;
13281 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13283 unsigned long r_symndx;
13285 if (! rcookie->bad_symtab)
13286 if (rcookie->rel->r_offset > offset)
13288 if (rcookie->rel->r_offset != offset)
13291 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13292 if (r_symndx == STN_UNDEF)
13295 if (r_symndx >= rcookie->locsymcount
13296 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13298 struct elf_link_hash_entry *h;
13300 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13302 while (h->root.type == bfd_link_hash_indirect
13303 || h->root.type == bfd_link_hash_warning)
13304 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13306 if ((h->root.type == bfd_link_hash_defined
13307 || h->root.type == bfd_link_hash_defweak)
13308 && (h->root.u.def.section->owner != rcookie->abfd
13309 || h->root.u.def.section->kept_section != NULL
13310 || discarded_section (h->root.u.def.section)))
13315 /* It's not a relocation against a global symbol,
13316 but it could be a relocation against a local
13317 symbol for a discarded section. */
13319 Elf_Internal_Sym *isym;
13321 /* Need to: get the symbol; get the section. */
13322 isym = &rcookie->locsyms[r_symndx];
13323 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13325 && (isec->kept_section != NULL
13326 || discarded_section (isec)))
13334 /* Discard unneeded references to discarded sections.
13335 Returns -1 on error, 1 if any section's size was changed, 0 if
13336 nothing changed. This function assumes that the relocations are in
13337 sorted order, which is true for all known assemblers. */
13340 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13342 struct elf_reloc_cookie cookie;
13347 if (info->traditional_format
13348 || !is_elf_hash_table (info->hash))
13351 o = bfd_get_section_by_name (output_bfd, ".stab");
13356 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13359 || i->reloc_count == 0
13360 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13364 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13367 if (!init_reloc_cookie_for_section (&cookie, info, i))
13370 if (_bfd_discard_section_stabs (abfd, i,
13371 elf_section_data (i)->sec_info,
13372 bfd_elf_reloc_symbol_deleted_p,
13376 fini_reloc_cookie_for_section (&cookie, i);
13381 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13382 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13387 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13393 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13396 if (!init_reloc_cookie_for_section (&cookie, info, i))
13399 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13400 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13401 bfd_elf_reloc_symbol_deleted_p,
13405 fini_reloc_cookie_for_section (&cookie, i);
13409 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13411 const struct elf_backend_data *bed;
13413 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13416 bed = get_elf_backend_data (abfd);
13418 if (bed->elf_backend_discard_info != NULL)
13420 if (!init_reloc_cookie (&cookie, info, abfd))
13423 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13426 fini_reloc_cookie (&cookie, abfd);
13430 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13431 _bfd_elf_end_eh_frame_parsing (info);
13433 if (info->eh_frame_hdr_type
13434 && !bfd_link_relocatable (info)
13435 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13442 _bfd_elf_section_already_linked (bfd *abfd,
13444 struct bfd_link_info *info)
13447 const char *name, *key;
13448 struct bfd_section_already_linked *l;
13449 struct bfd_section_already_linked_hash_entry *already_linked_list;
13451 if (sec->output_section == bfd_abs_section_ptr)
13454 flags = sec->flags;
13456 /* Return if it isn't a linkonce section. A comdat group section
13457 also has SEC_LINK_ONCE set. */
13458 if ((flags & SEC_LINK_ONCE) == 0)
13461 /* Don't put group member sections on our list of already linked
13462 sections. They are handled as a group via their group section. */
13463 if (elf_sec_group (sec) != NULL)
13466 /* For a SHT_GROUP section, use the group signature as the key. */
13468 if ((flags & SEC_GROUP) != 0
13469 && elf_next_in_group (sec) != NULL
13470 && elf_group_name (elf_next_in_group (sec)) != NULL)
13471 key = elf_group_name (elf_next_in_group (sec));
13474 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13475 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13476 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13479 /* Must be a user linkonce section that doesn't follow gcc's
13480 naming convention. In this case we won't be matching
13481 single member groups. */
13485 already_linked_list = bfd_section_already_linked_table_lookup (key);
13487 for (l = already_linked_list->entry; l != NULL; l = l->next)
13489 /* We may have 2 different types of sections on the list: group
13490 sections with a signature of <key> (<key> is some string),
13491 and linkonce sections named .gnu.linkonce.<type>.<key>.
13492 Match like sections. LTO plugin sections are an exception.
13493 They are always named .gnu.linkonce.t.<key> and match either
13494 type of section. */
13495 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13496 && ((flags & SEC_GROUP) != 0
13497 || strcmp (name, l->sec->name) == 0))
13498 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13500 /* The section has already been linked. See if we should
13501 issue a warning. */
13502 if (!_bfd_handle_already_linked (sec, l, info))
13505 if (flags & SEC_GROUP)
13507 asection *first = elf_next_in_group (sec);
13508 asection *s = first;
13512 s->output_section = bfd_abs_section_ptr;
13513 /* Record which group discards it. */
13514 s->kept_section = l->sec;
13515 s = elf_next_in_group (s);
13516 /* These lists are circular. */
13526 /* A single member comdat group section may be discarded by a
13527 linkonce section and vice versa. */
13528 if ((flags & SEC_GROUP) != 0)
13530 asection *first = elf_next_in_group (sec);
13532 if (first != NULL && elf_next_in_group (first) == first)
13533 /* Check this single member group against linkonce sections. */
13534 for (l = already_linked_list->entry; l != NULL; l = l->next)
13535 if ((l->sec->flags & SEC_GROUP) == 0
13536 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13538 first->output_section = bfd_abs_section_ptr;
13539 first->kept_section = l->sec;
13540 sec->output_section = bfd_abs_section_ptr;
13545 /* Check this linkonce section against single member groups. */
13546 for (l = already_linked_list->entry; l != NULL; l = l->next)
13547 if (l->sec->flags & SEC_GROUP)
13549 asection *first = elf_next_in_group (l->sec);
13552 && elf_next_in_group (first) == first
13553 && bfd_elf_match_symbols_in_sections (first, sec, info))
13555 sec->output_section = bfd_abs_section_ptr;
13556 sec->kept_section = first;
13561 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13562 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13563 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13564 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13565 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13566 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13567 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13568 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13569 The reverse order cannot happen as there is never a bfd with only the
13570 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13571 matter as here were are looking only for cross-bfd sections. */
13573 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13574 for (l = already_linked_list->entry; l != NULL; l = l->next)
13575 if ((l->sec->flags & SEC_GROUP) == 0
13576 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13578 if (abfd != l->sec->owner)
13579 sec->output_section = bfd_abs_section_ptr;
13583 /* This is the first section with this name. Record it. */
13584 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13585 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13586 return sec->output_section == bfd_abs_section_ptr;
13590 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13592 return sym->st_shndx == SHN_COMMON;
13596 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13602 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13604 return bfd_com_section_ptr;
13608 _bfd_elf_default_got_elt_size (bfd *abfd,
13609 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13610 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13611 bfd *ibfd ATTRIBUTE_UNUSED,
13612 unsigned long symndx ATTRIBUTE_UNUSED)
13614 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13615 return bed->s->arch_size / 8;
13618 /* Routines to support the creation of dynamic relocs. */
13620 /* Returns the name of the dynamic reloc section associated with SEC. */
13622 static const char *
13623 get_dynamic_reloc_section_name (bfd * abfd,
13625 bfd_boolean is_rela)
13628 const char *old_name = bfd_get_section_name (NULL, sec);
13629 const char *prefix = is_rela ? ".rela" : ".rel";
13631 if (old_name == NULL)
13634 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13635 sprintf (name, "%s%s", prefix, old_name);
13640 /* Returns the dynamic reloc section associated with SEC.
13641 If necessary compute the name of the dynamic reloc section based
13642 on SEC's name (looked up in ABFD's string table) and the setting
13646 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13648 bfd_boolean is_rela)
13650 asection * reloc_sec = elf_section_data (sec)->sreloc;
13652 if (reloc_sec == NULL)
13654 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13658 reloc_sec = bfd_get_linker_section (abfd, name);
13660 if (reloc_sec != NULL)
13661 elf_section_data (sec)->sreloc = reloc_sec;
13668 /* Returns the dynamic reloc section associated with SEC. If the
13669 section does not exist it is created and attached to the DYNOBJ
13670 bfd and stored in the SRELOC field of SEC's elf_section_data
13673 ALIGNMENT is the alignment for the newly created section and
13674 IS_RELA defines whether the name should be .rela.<SEC's name>
13675 or .rel.<SEC's name>. The section name is looked up in the
13676 string table associated with ABFD. */
13679 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13681 unsigned int alignment,
13683 bfd_boolean is_rela)
13685 asection * reloc_sec = elf_section_data (sec)->sreloc;
13687 if (reloc_sec == NULL)
13689 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13694 reloc_sec = bfd_get_linker_section (dynobj, name);
13696 if (reloc_sec == NULL)
13698 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13699 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13700 if ((sec->flags & SEC_ALLOC) != 0)
13701 flags |= SEC_ALLOC | SEC_LOAD;
13703 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13704 if (reloc_sec != NULL)
13706 /* _bfd_elf_get_sec_type_attr chooses a section type by
13707 name. Override as it may be wrong, eg. for a user
13708 section named "auto" we'll get ".relauto" which is
13709 seen to be a .rela section. */
13710 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13711 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13716 elf_section_data (sec)->sreloc = reloc_sec;
13722 /* Copy the ELF symbol type and other attributes for a linker script
13723 assignment from HSRC to HDEST. Generally this should be treated as
13724 if we found a strong non-dynamic definition for HDEST (except that
13725 ld ignores multiple definition errors). */
13727 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13728 struct bfd_link_hash_entry *hdest,
13729 struct bfd_link_hash_entry *hsrc)
13731 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13732 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13733 Elf_Internal_Sym isym;
13735 ehdest->type = ehsrc->type;
13736 ehdest->target_internal = ehsrc->target_internal;
13738 isym.st_other = ehsrc->other;
13739 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13742 /* Append a RELA relocation REL to section S in BFD. */
13745 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13747 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13748 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13749 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13750 bed->s->swap_reloca_out (abfd, rel, loc);
13753 /* Append a REL relocation REL to section S in BFD. */
13756 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13758 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13759 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13760 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13761 bed->s->swap_reloc_out (abfd, rel, loc);