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)
207 hash_table->dynobj = abfd;
209 if (hash_table->dynstr == NULL)
211 hash_table->dynstr = _bfd_elf_strtab_init ();
212 if (hash_table->dynstr == NULL)
218 /* Create some sections which will be filled in with dynamic linking
219 information. ABFD is an input file which requires dynamic sections
220 to be created. The dynamic sections take up virtual memory space
221 when the final executable is run, so we need to create them before
222 addresses are assigned to the output sections. We work out the
223 actual contents and size of these sections later. */
226 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
230 const struct elf_backend_data *bed;
231 struct elf_link_hash_entry *h;
233 if (! is_elf_hash_table (info->hash))
236 if (elf_hash_table (info)->dynamic_sections_created)
239 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
242 abfd = elf_hash_table (info)->dynobj;
243 bed = get_elf_backend_data (abfd);
245 flags = bed->dynamic_sec_flags;
247 /* A dynamically linked executable has a .interp section, but a
248 shared library does not. */
249 if (bfd_link_executable (info) && !info->nointerp)
251 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
252 flags | SEC_READONLY);
257 /* Create sections to hold version informations. These are removed
258 if they are not needed. */
259 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
260 flags | SEC_READONLY);
262 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
265 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
266 flags | SEC_READONLY);
268 || ! bfd_set_section_alignment (abfd, s, 1))
271 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
272 flags | SEC_READONLY);
274 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
277 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
278 flags | SEC_READONLY);
280 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
282 elf_hash_table (info)->dynsym = s;
284 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
285 flags | SEC_READONLY);
289 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
294 /* The special symbol _DYNAMIC is always set to the start of the
295 .dynamic section. We could set _DYNAMIC in a linker script, but we
296 only want to define it if we are, in fact, creating a .dynamic
297 section. We don't want to define it if there is no .dynamic
298 section, since on some ELF platforms the start up code examines it
299 to decide how to initialize the process. */
300 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
301 elf_hash_table (info)->hdynamic = h;
307 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
308 flags | SEC_READONLY);
310 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
315 if (info->emit_gnu_hash)
317 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
318 flags | SEC_READONLY);
320 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
322 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
323 4 32-bit words followed by variable count of 64-bit words, then
324 variable count of 32-bit words. */
325 if (bed->s->arch_size == 64)
326 elf_section_data (s)->this_hdr.sh_entsize = 0;
328 elf_section_data (s)->this_hdr.sh_entsize = 4;
331 /* Let the backend create the rest of the sections. This lets the
332 backend set the right flags. The backend will normally create
333 the .got and .plt sections. */
334 if (bed->elf_backend_create_dynamic_sections == NULL
335 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
338 elf_hash_table (info)->dynamic_sections_created = TRUE;
343 /* Create dynamic sections when linking against a dynamic object. */
346 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
348 flagword flags, pltflags;
349 struct elf_link_hash_entry *h;
351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
352 struct elf_link_hash_table *htab = elf_hash_table (info);
354 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
355 .rel[a].bss sections. */
356 flags = bed->dynamic_sec_flags;
359 if (bed->plt_not_loaded)
360 /* We do not clear SEC_ALLOC here because we still want the OS to
361 allocate space for the section; it's just that there's nothing
362 to read in from the object file. */
363 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
365 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
366 if (bed->plt_readonly)
367 pltflags |= SEC_READONLY;
369 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
371 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
375 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
377 if (bed->want_plt_sym)
379 h = _bfd_elf_define_linkage_sym (abfd, info, s,
380 "_PROCEDURE_LINKAGE_TABLE_");
381 elf_hash_table (info)->hplt = h;
386 s = bfd_make_section_anyway_with_flags (abfd,
387 (bed->rela_plts_and_copies_p
388 ? ".rela.plt" : ".rel.plt"),
389 flags | SEC_READONLY);
391 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
395 if (! _bfd_elf_create_got_section (abfd, info))
398 if (bed->want_dynbss)
400 /* The .dynbss section is a place to put symbols which are defined
401 by dynamic objects, are referenced by regular objects, and are
402 not functions. We must allocate space for them in the process
403 image and use a R_*_COPY reloc to tell the dynamic linker to
404 initialize them at run time. The linker script puts the .dynbss
405 section into the .bss section of the final image. */
406 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
407 (SEC_ALLOC | SEC_LINKER_CREATED));
411 /* The .rel[a].bss section holds copy relocs. This section is not
412 normally needed. We need to create it here, though, so that the
413 linker will map it to an output section. We can't just create it
414 only if we need it, because we will not know whether we need it
415 until we have seen all the input files, and the first time the
416 main linker code calls BFD after examining all the input files
417 (size_dynamic_sections) the input sections have already been
418 mapped to the output sections. If the section turns out not to
419 be needed, we can discard it later. We will never need this
420 section when generating a shared object, since they do not use
422 if (! bfd_link_pic (info))
424 s = bfd_make_section_anyway_with_flags (abfd,
425 (bed->rela_plts_and_copies_p
426 ? ".rela.bss" : ".rel.bss"),
427 flags | SEC_READONLY);
429 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
437 /* Record a new dynamic symbol. We record the dynamic symbols as we
438 read the input files, since we need to have a list of all of them
439 before we can determine the final sizes of the output sections.
440 Note that we may actually call this function even though we are not
441 going to output any dynamic symbols; in some cases we know that a
442 symbol should be in the dynamic symbol table, but only if there is
446 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
447 struct elf_link_hash_entry *h)
449 if (h->dynindx == -1)
451 struct elf_strtab_hash *dynstr;
456 /* XXX: The ABI draft says the linker must turn hidden and
457 internal symbols into STB_LOCAL symbols when producing the
458 DSO. However, if ld.so honors st_other in the dynamic table,
459 this would not be necessary. */
460 switch (ELF_ST_VISIBILITY (h->other))
464 if (h->root.type != bfd_link_hash_undefined
465 && h->root.type != bfd_link_hash_undefweak)
468 if (!elf_hash_table (info)->is_relocatable_executable)
476 h->dynindx = elf_hash_table (info)->dynsymcount;
477 ++elf_hash_table (info)->dynsymcount;
479 dynstr = elf_hash_table (info)->dynstr;
482 /* Create a strtab to hold the dynamic symbol names. */
483 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
488 /* We don't put any version information in the dynamic string
490 name = h->root.root.string;
491 p = strchr (name, ELF_VER_CHR);
493 /* We know that the p points into writable memory. In fact,
494 there are only a few symbols that have read-only names, being
495 those like _GLOBAL_OFFSET_TABLE_ that are created specially
496 by the backends. Most symbols will have names pointing into
497 an ELF string table read from a file, or to objalloc memory. */
500 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
505 if (indx == (bfd_size_type) -1)
507 h->dynstr_index = indx;
513 /* Mark a symbol dynamic. */
516 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
517 struct elf_link_hash_entry *h,
518 Elf_Internal_Sym *sym)
520 struct bfd_elf_dynamic_list *d = info->dynamic_list;
522 /* It may be called more than once on the same H. */
523 if(h->dynamic || bfd_link_relocatable (info))
526 if ((info->dynamic_data
527 && (h->type == STT_OBJECT
528 || h->type == STT_COMMON
530 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
531 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
533 && h->root.type == bfd_link_hash_new
534 && (*d->match) (&d->head, NULL, h->root.root.string)))
538 /* Record an assignment to a symbol made by a linker script. We need
539 this in case some dynamic object refers to this symbol. */
542 bfd_elf_record_link_assignment (bfd *output_bfd,
543 struct bfd_link_info *info,
548 struct elf_link_hash_entry *h, *hv;
549 struct elf_link_hash_table *htab;
550 const struct elf_backend_data *bed;
552 if (!is_elf_hash_table (info->hash))
555 htab = elf_hash_table (info);
556 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
560 if (h->versioned == unknown)
562 /* Set versioned if symbol version is unknown. */
563 char *version = strrchr (name, ELF_VER_CHR);
566 if (version > name && version[-1] != ELF_VER_CHR)
567 h->versioned = versioned_hidden;
569 h->versioned = versioned;
573 switch (h->root.type)
575 case bfd_link_hash_defined:
576 case bfd_link_hash_defweak:
577 case bfd_link_hash_common:
579 case bfd_link_hash_undefweak:
580 case bfd_link_hash_undefined:
581 /* Since we're defining the symbol, don't let it seem to have not
582 been defined. record_dynamic_symbol and size_dynamic_sections
583 may depend on this. */
584 h->root.type = bfd_link_hash_new;
585 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
586 bfd_link_repair_undef_list (&htab->root);
588 case bfd_link_hash_new:
589 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
592 case bfd_link_hash_indirect:
593 /* We had a versioned symbol in a dynamic library. We make the
594 the versioned symbol point to this one. */
595 bed = get_elf_backend_data (output_bfd);
597 while (hv->root.type == bfd_link_hash_indirect
598 || hv->root.type == bfd_link_hash_warning)
599 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
600 /* We don't need to update h->root.u since linker will set them
602 h->root.type = bfd_link_hash_undefined;
603 hv->root.type = bfd_link_hash_indirect;
604 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
605 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
607 case bfd_link_hash_warning:
612 /* If this symbol is being provided by the linker script, and it is
613 currently defined by a dynamic object, but not by a regular
614 object, then mark it as undefined so that the generic linker will
615 force the correct value. */
619 h->root.type = bfd_link_hash_undefined;
621 /* If this symbol is not being provided by the linker script, and it is
622 currently defined by a dynamic object, but not by a regular object,
623 then clear out any version information because the symbol will not be
624 associated with the dynamic object any more. */
628 h->verinfo.verdef = NULL;
634 bed = get_elf_backend_data (output_bfd);
635 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
636 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
637 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
640 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
642 if (!bfd_link_relocatable (info)
644 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
645 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
650 || bfd_link_dll (info)
651 || elf_hash_table (info)->is_relocatable_executable)
654 if (! bfd_elf_link_record_dynamic_symbol (info, h))
657 /* If this is a weak defined symbol, and we know a corresponding
658 real symbol from the same dynamic object, make sure the real
659 symbol is also made into a dynamic symbol. */
660 if (h->u.weakdef != NULL
661 && h->u.weakdef->dynindx == -1)
663 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
671 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
672 success, and 2 on a failure caused by attempting to record a symbol
673 in a discarded section, eg. a discarded link-once section symbol. */
676 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
681 struct elf_link_local_dynamic_entry *entry;
682 struct elf_link_hash_table *eht;
683 struct elf_strtab_hash *dynstr;
684 unsigned long dynstr_index;
686 Elf_External_Sym_Shndx eshndx;
687 char esym[sizeof (Elf64_External_Sym)];
689 if (! is_elf_hash_table (info->hash))
692 /* See if the entry exists already. */
693 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
694 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
697 amt = sizeof (*entry);
698 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
702 /* Go find the symbol, so that we can find it's name. */
703 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
704 1, input_indx, &entry->isym, esym, &eshndx))
706 bfd_release (input_bfd, entry);
710 if (entry->isym.st_shndx != SHN_UNDEF
711 && entry->isym.st_shndx < SHN_LORESERVE)
715 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
716 if (s == NULL || bfd_is_abs_section (s->output_section))
718 /* We can still bfd_release here as nothing has done another
719 bfd_alloc. We can't do this later in this function. */
720 bfd_release (input_bfd, entry);
725 name = (bfd_elf_string_from_elf_section
726 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
727 entry->isym.st_name));
729 dynstr = elf_hash_table (info)->dynstr;
732 /* Create a strtab to hold the dynamic symbol names. */
733 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
738 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
739 if (dynstr_index == (unsigned long) -1)
741 entry->isym.st_name = dynstr_index;
743 eht = elf_hash_table (info);
745 entry->next = eht->dynlocal;
746 eht->dynlocal = entry;
747 entry->input_bfd = input_bfd;
748 entry->input_indx = input_indx;
751 /* Whatever binding the symbol had before, it's now local. */
753 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
755 /* The dynindx will be set at the end of size_dynamic_sections. */
760 /* Return the dynindex of a local dynamic symbol. */
763 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
767 struct elf_link_local_dynamic_entry *e;
769 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
770 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
775 /* This function is used to renumber the dynamic symbols, if some of
776 them are removed because they are marked as local. This is called
777 via elf_link_hash_traverse. */
780 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
783 size_t *count = (size_t *) data;
788 if (h->dynindx != -1)
789 h->dynindx = ++(*count);
795 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
796 STB_LOCAL binding. */
799 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
802 size_t *count = (size_t *) data;
804 if (!h->forced_local)
807 if (h->dynindx != -1)
808 h->dynindx = ++(*count);
813 /* Return true if the dynamic symbol for a given section should be
814 omitted when creating a shared library. */
816 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
817 struct bfd_link_info *info,
820 struct elf_link_hash_table *htab;
823 switch (elf_section_data (p)->this_hdr.sh_type)
827 /* If sh_type is yet undecided, assume it could be
828 SHT_PROGBITS/SHT_NOBITS. */
830 htab = elf_hash_table (info);
831 if (p == htab->tls_sec)
834 if (htab->text_index_section != NULL)
835 return p != htab->text_index_section && p != htab->data_index_section;
837 return (htab->dynobj != NULL
838 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
839 && ip->output_section == p);
841 /* There shouldn't be section relative relocations
842 against any other section. */
848 /* Assign dynsym indices. In a shared library we generate a section
849 symbol for each output section, which come first. Next come symbols
850 which have been forced to local binding. Then all of the back-end
851 allocated local dynamic syms, followed by the rest of the global
855 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
856 struct bfd_link_info *info,
857 unsigned long *section_sym_count)
859 unsigned long dynsymcount = 0;
861 if (bfd_link_pic (info)
862 || elf_hash_table (info)->is_relocatable_executable)
864 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
866 for (p = output_bfd->sections; p ; p = p->next)
867 if ((p->flags & SEC_EXCLUDE) == 0
868 && (p->flags & SEC_ALLOC) != 0
869 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
870 elf_section_data (p)->dynindx = ++dynsymcount;
872 elf_section_data (p)->dynindx = 0;
874 *section_sym_count = dynsymcount;
876 elf_link_hash_traverse (elf_hash_table (info),
877 elf_link_renumber_local_hash_table_dynsyms,
880 if (elf_hash_table (info)->dynlocal)
882 struct elf_link_local_dynamic_entry *p;
883 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
884 p->dynindx = ++dynsymcount;
887 elf_link_hash_traverse (elf_hash_table (info),
888 elf_link_renumber_hash_table_dynsyms,
891 /* There is an unused NULL entry at the head of the table which
892 we must account for in our count. We always create the dynsym
893 section, even if it is empty, with dynamic sections. */
894 if (elf_hash_table (info)->dynamic_sections_created)
897 elf_hash_table (info)->dynsymcount = dynsymcount;
901 /* Merge st_other field. */
904 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
905 const Elf_Internal_Sym *isym, asection *sec,
906 bfd_boolean definition, bfd_boolean dynamic)
908 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
910 /* If st_other has a processor-specific meaning, specific
911 code might be needed here. */
912 if (bed->elf_backend_merge_symbol_attribute)
913 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
918 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
919 unsigned hvis = ELF_ST_VISIBILITY (h->other);
921 /* Keep the most constraining visibility. Leave the remainder
922 of the st_other field to elf_backend_merge_symbol_attribute. */
923 if (symvis - 1 < hvis - 1)
924 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
927 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
928 && (sec->flags & SEC_READONLY) == 0)
929 h->protected_def = 1;
932 /* This function is called when we want to merge a new symbol with an
933 existing symbol. It handles the various cases which arise when we
934 find a definition in a dynamic object, or when there is already a
935 definition in a dynamic object. The new symbol is described by
936 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
937 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
938 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
939 of an old common symbol. We set OVERRIDE if the old symbol is
940 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
941 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
942 to change. By OK to change, we mean that we shouldn't warn if the
943 type or size does change. */
946 _bfd_elf_merge_symbol (bfd *abfd,
947 struct bfd_link_info *info,
949 Elf_Internal_Sym *sym,
952 struct elf_link_hash_entry **sym_hash,
954 bfd_boolean *pold_weak,
955 unsigned int *pold_alignment,
957 bfd_boolean *override,
958 bfd_boolean *type_change_ok,
959 bfd_boolean *size_change_ok,
960 bfd_boolean *matched)
962 asection *sec, *oldsec;
963 struct elf_link_hash_entry *h;
964 struct elf_link_hash_entry *hi;
965 struct elf_link_hash_entry *flip;
968 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
969 bfd_boolean newweak, oldweak, newfunc, oldfunc;
970 const struct elf_backend_data *bed;
977 bind = ELF_ST_BIND (sym->st_info);
979 if (! bfd_is_und_section (sec))
980 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
982 h = ((struct elf_link_hash_entry *)
983 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
988 bed = get_elf_backend_data (abfd);
990 /* NEW_VERSION is the symbol version of the new symbol. */
991 if (h->versioned != unversioned)
993 /* Symbol version is unknown or versioned. */
994 new_version = strrchr (name, ELF_VER_CHR);
997 if (h->versioned == unknown)
999 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1000 h->versioned = versioned_hidden;
1002 h->versioned = versioned;
1005 if (new_version[0] == '\0')
1009 h->versioned = unversioned;
1014 /* For merging, we only care about real symbols. But we need to make
1015 sure that indirect symbol dynamic flags are updated. */
1017 while (h->root.type == bfd_link_hash_indirect
1018 || h->root.type == bfd_link_hash_warning)
1019 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1023 if (hi == h || h->root.type == bfd_link_hash_new)
1027 /* OLD_HIDDEN is true if the existing symbol is only visible
1028 to the symbol with the same symbol version. NEW_HIDDEN is
1029 true if the new symbol is only visible to the symbol with
1030 the same symbol version. */
1031 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1032 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1033 if (!old_hidden && !new_hidden)
1034 /* The new symbol matches the existing symbol if both
1039 /* OLD_VERSION is the symbol version of the existing
1043 if (h->versioned >= versioned)
1044 old_version = strrchr (h->root.root.string,
1049 /* The new symbol matches the existing symbol if they
1050 have the same symbol version. */
1051 *matched = (old_version == new_version
1052 || (old_version != NULL
1053 && new_version != NULL
1054 && strcmp (old_version, new_version) == 0));
1059 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1064 switch (h->root.type)
1069 case bfd_link_hash_undefined:
1070 case bfd_link_hash_undefweak:
1071 oldbfd = h->root.u.undef.abfd;
1074 case bfd_link_hash_defined:
1075 case bfd_link_hash_defweak:
1076 oldbfd = h->root.u.def.section->owner;
1077 oldsec = h->root.u.def.section;
1080 case bfd_link_hash_common:
1081 oldbfd = h->root.u.c.p->section->owner;
1082 oldsec = h->root.u.c.p->section;
1084 *pold_alignment = h->root.u.c.p->alignment_power;
1087 if (poldbfd && *poldbfd == NULL)
1090 /* Differentiate strong and weak symbols. */
1091 newweak = bind == STB_WEAK;
1092 oldweak = (h->root.type == bfd_link_hash_defweak
1093 || h->root.type == bfd_link_hash_undefweak);
1095 *pold_weak = oldweak;
1097 /* This code is for coping with dynamic objects, and is only useful
1098 if we are doing an ELF link. */
1099 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1102 /* We have to check it for every instance since the first few may be
1103 references and not all compilers emit symbol type for undefined
1105 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1107 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1108 respectively, is from a dynamic object. */
1110 newdyn = (abfd->flags & DYNAMIC) != 0;
1112 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1113 syms and defined syms in dynamic libraries respectively.
1114 ref_dynamic on the other hand can be set for a symbol defined in
1115 a dynamic library, and def_dynamic may not be set; When the
1116 definition in a dynamic lib is overridden by a definition in the
1117 executable use of the symbol in the dynamic lib becomes a
1118 reference to the executable symbol. */
1121 if (bfd_is_und_section (sec))
1123 if (bind != STB_WEAK)
1125 h->ref_dynamic_nonweak = 1;
1126 hi->ref_dynamic_nonweak = 1;
1131 /* Update the existing symbol only if they match. */
1134 hi->dynamic_def = 1;
1138 /* If we just created the symbol, mark it as being an ELF symbol.
1139 Other than that, there is nothing to do--there is no merge issue
1140 with a newly defined symbol--so we just return. */
1142 if (h->root.type == bfd_link_hash_new)
1148 /* In cases involving weak versioned symbols, we may wind up trying
1149 to merge a symbol with itself. Catch that here, to avoid the
1150 confusion that results if we try to override a symbol with
1151 itself. The additional tests catch cases like
1152 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1153 dynamic object, which we do want to handle here. */
1155 && (newweak || oldweak)
1156 && ((abfd->flags & DYNAMIC) == 0
1157 || !h->def_regular))
1162 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1163 else if (oldsec != NULL)
1165 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1166 indices used by MIPS ELF. */
1167 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1170 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1171 respectively, appear to be a definition rather than reference. */
1173 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1175 olddef = (h->root.type != bfd_link_hash_undefined
1176 && h->root.type != bfd_link_hash_undefweak
1177 && h->root.type != bfd_link_hash_common);
1179 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1180 respectively, appear to be a function. */
1182 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1183 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1185 oldfunc = (h->type != STT_NOTYPE
1186 && bed->is_function_type (h->type));
1188 /* When we try to create a default indirect symbol from the dynamic
1189 definition with the default version, we skip it if its type and
1190 the type of existing regular definition mismatch. */
1191 if (pold_alignment == NULL
1195 && (((olddef || h->root.type == bfd_link_hash_common)
1196 && ELF_ST_TYPE (sym->st_info) != h->type
1197 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1198 && h->type != STT_NOTYPE
1199 && !(newfunc && oldfunc))
1201 && ((h->type == STT_GNU_IFUNC)
1202 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
1208 /* Check TLS symbols. We don't check undefined symbols introduced
1209 by "ld -u" which have no type (and oldbfd NULL), and we don't
1210 check symbols from plugins because they also have no type. */
1212 && (oldbfd->flags & BFD_PLUGIN) == 0
1213 && (abfd->flags & BFD_PLUGIN) == 0
1214 && ELF_ST_TYPE (sym->st_info) != h->type
1215 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1218 bfd_boolean ntdef, tdef;
1219 asection *ntsec, *tsec;
1221 if (h->type == STT_TLS)
1241 (*_bfd_error_handler)
1242 (_("%s: TLS definition in %B section %A "
1243 "mismatches non-TLS definition in %B section %A"),
1244 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1245 else if (!tdef && !ntdef)
1246 (*_bfd_error_handler)
1247 (_("%s: TLS reference in %B "
1248 "mismatches non-TLS reference in %B"),
1249 tbfd, ntbfd, h->root.root.string);
1251 (*_bfd_error_handler)
1252 (_("%s: TLS definition in %B section %A "
1253 "mismatches non-TLS reference in %B"),
1254 tbfd, tsec, ntbfd, h->root.root.string);
1256 (*_bfd_error_handler)
1257 (_("%s: TLS reference in %B "
1258 "mismatches non-TLS definition in %B section %A"),
1259 tbfd, ntbfd, ntsec, h->root.root.string);
1261 bfd_set_error (bfd_error_bad_value);
1265 /* If the old symbol has non-default visibility, we ignore the new
1266 definition from a dynamic object. */
1268 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1269 && !bfd_is_und_section (sec))
1272 /* Make sure this symbol is dynamic. */
1274 hi->ref_dynamic = 1;
1275 /* A protected symbol has external availability. Make sure it is
1276 recorded as dynamic.
1278 FIXME: Should we check type and size for protected symbol? */
1279 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1280 return bfd_elf_link_record_dynamic_symbol (info, h);
1285 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1288 /* If the new symbol with non-default visibility comes from a
1289 relocatable file and the old definition comes from a dynamic
1290 object, we remove the old definition. */
1291 if (hi->root.type == bfd_link_hash_indirect)
1293 /* Handle the case where the old dynamic definition is
1294 default versioned. We need to copy the symbol info from
1295 the symbol with default version to the normal one if it
1296 was referenced before. */
1299 hi->root.type = h->root.type;
1300 h->root.type = bfd_link_hash_indirect;
1301 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1303 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1304 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1306 /* If the new symbol is hidden or internal, completely undo
1307 any dynamic link state. */
1308 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 h->forced_local = 0;
1316 /* FIXME: Should we check type and size for protected symbol? */
1326 /* If the old symbol was undefined before, then it will still be
1327 on the undefs list. If the new symbol is undefined or
1328 common, we can't make it bfd_link_hash_new here, because new
1329 undefined or common symbols will be added to the undefs list
1330 by _bfd_generic_link_add_one_symbol. Symbols may not be
1331 added twice to the undefs list. Also, if the new symbol is
1332 undefweak then we don't want to lose the strong undef. */
1333 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1335 h->root.type = bfd_link_hash_undefined;
1336 h->root.u.undef.abfd = abfd;
1340 h->root.type = bfd_link_hash_new;
1341 h->root.u.undef.abfd = NULL;
1344 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1346 /* If the new symbol is hidden or internal, completely undo
1347 any dynamic link state. */
1348 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1349 h->forced_local = 0;
1355 /* FIXME: Should we check type and size for protected symbol? */
1361 /* If a new weak symbol definition comes from a regular file and the
1362 old symbol comes from a dynamic library, we treat the new one as
1363 strong. Similarly, an old weak symbol definition from a regular
1364 file is treated as strong when the new symbol comes from a dynamic
1365 library. Further, an old weak symbol from a dynamic library is
1366 treated as strong if the new symbol is from a dynamic library.
1367 This reflects the way glibc's ld.so works.
1369 Do this before setting *type_change_ok or *size_change_ok so that
1370 we warn properly when dynamic library symbols are overridden. */
1372 if (newdef && !newdyn && olddyn)
1374 if (olddef && newdyn)
1377 /* Allow changes between different types of function symbol. */
1378 if (newfunc && oldfunc)
1379 *type_change_ok = TRUE;
1381 /* It's OK to change the type if either the existing symbol or the
1382 new symbol is weak. A type change is also OK if the old symbol
1383 is undefined and the new symbol is defined. */
1388 && h->root.type == bfd_link_hash_undefined))
1389 *type_change_ok = TRUE;
1391 /* It's OK to change the size if either the existing symbol or the
1392 new symbol is weak, or if the old symbol is undefined. */
1395 || h->root.type == bfd_link_hash_undefined)
1396 *size_change_ok = TRUE;
1398 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1399 symbol, respectively, appears to be a common symbol in a dynamic
1400 object. If a symbol appears in an uninitialized section, and is
1401 not weak, and is not a function, then it may be a common symbol
1402 which was resolved when the dynamic object was created. We want
1403 to treat such symbols specially, because they raise special
1404 considerations when setting the symbol size: if the symbol
1405 appears as a common symbol in a regular object, and the size in
1406 the regular object is larger, we must make sure that we use the
1407 larger size. This problematic case can always be avoided in C,
1408 but it must be handled correctly when using Fortran shared
1411 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1412 likewise for OLDDYNCOMMON and OLDDEF.
1414 Note that this test is just a heuristic, and that it is quite
1415 possible to have an uninitialized symbol in a shared object which
1416 is really a definition, rather than a common symbol. This could
1417 lead to some minor confusion when the symbol really is a common
1418 symbol in some regular object. However, I think it will be
1424 && (sec->flags & SEC_ALLOC) != 0
1425 && (sec->flags & SEC_LOAD) == 0
1428 newdyncommon = TRUE;
1430 newdyncommon = FALSE;
1434 && h->root.type == bfd_link_hash_defined
1436 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1437 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1440 olddyncommon = TRUE;
1442 olddyncommon = FALSE;
1444 /* We now know everything about the old and new symbols. We ask the
1445 backend to check if we can merge them. */
1446 if (bed->merge_symbol != NULL)
1448 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1453 /* If both the old and the new symbols look like common symbols in a
1454 dynamic object, set the size of the symbol to the larger of the
1459 && sym->st_size != h->size)
1461 /* Since we think we have two common symbols, issue a multiple
1462 common warning if desired. Note that we only warn if the
1463 size is different. If the size is the same, we simply let
1464 the old symbol override the new one as normally happens with
1465 symbols defined in dynamic objects. */
1467 if (! ((*info->callbacks->multiple_common)
1468 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1471 if (sym->st_size > h->size)
1472 h->size = sym->st_size;
1474 *size_change_ok = TRUE;
1477 /* If we are looking at a dynamic object, and we have found a
1478 definition, we need to see if the symbol was already defined by
1479 some other object. If so, we want to use the existing
1480 definition, and we do not want to report a multiple symbol
1481 definition error; we do this by clobbering *PSEC to be
1482 bfd_und_section_ptr.
1484 We treat a common symbol as a definition if the symbol in the
1485 shared library is a function, since common symbols always
1486 represent variables; this can cause confusion in principle, but
1487 any such confusion would seem to indicate an erroneous program or
1488 shared library. We also permit a common symbol in a regular
1489 object to override a weak symbol in a shared object. */
1494 || (h->root.type == bfd_link_hash_common
1495 && (newweak || newfunc))))
1499 newdyncommon = FALSE;
1501 *psec = sec = bfd_und_section_ptr;
1502 *size_change_ok = TRUE;
1504 /* If we get here when the old symbol is a common symbol, then
1505 we are explicitly letting it override a weak symbol or
1506 function in a dynamic object, and we don't want to warn about
1507 a type change. If the old symbol is a defined symbol, a type
1508 change warning may still be appropriate. */
1510 if (h->root.type == bfd_link_hash_common)
1511 *type_change_ok = TRUE;
1514 /* Handle the special case of an old common symbol merging with a
1515 new symbol which looks like a common symbol in a shared object.
1516 We change *PSEC and *PVALUE to make the new symbol look like a
1517 common symbol, and let _bfd_generic_link_add_one_symbol do the
1521 && h->root.type == bfd_link_hash_common)
1525 newdyncommon = FALSE;
1526 *pvalue = sym->st_size;
1527 *psec = sec = bed->common_section (oldsec);
1528 *size_change_ok = TRUE;
1531 /* Skip weak definitions of symbols that are already defined. */
1532 if (newdef && olddef && newweak)
1534 /* Don't skip new non-IR weak syms. */
1535 if (!(oldbfd != NULL
1536 && (oldbfd->flags & BFD_PLUGIN) != 0
1537 && (abfd->flags & BFD_PLUGIN) == 0))
1543 /* Merge st_other. If the symbol already has a dynamic index,
1544 but visibility says it should not be visible, turn it into a
1546 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1547 if (h->dynindx != -1)
1548 switch (ELF_ST_VISIBILITY (h->other))
1552 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1557 /* If the old symbol is from a dynamic object, and the new symbol is
1558 a definition which is not from a dynamic object, then the new
1559 symbol overrides the old symbol. Symbols from regular files
1560 always take precedence over symbols from dynamic objects, even if
1561 they are defined after the dynamic object in the link.
1563 As above, we again permit a common symbol in a regular object to
1564 override a definition in a shared object if the shared object
1565 symbol is a function or is weak. */
1570 || (bfd_is_com_section (sec)
1571 && (oldweak || oldfunc)))
1576 /* Change the hash table entry to undefined, and let
1577 _bfd_generic_link_add_one_symbol do the right thing with the
1580 h->root.type = bfd_link_hash_undefined;
1581 h->root.u.undef.abfd = h->root.u.def.section->owner;
1582 *size_change_ok = TRUE;
1585 olddyncommon = FALSE;
1587 /* We again permit a type change when a common symbol may be
1588 overriding a function. */
1590 if (bfd_is_com_section (sec))
1594 /* If a common symbol overrides a function, make sure
1595 that it isn't defined dynamically nor has type
1598 h->type = STT_NOTYPE;
1600 *type_change_ok = TRUE;
1603 if (hi->root.type == bfd_link_hash_indirect)
1606 /* This union may have been set to be non-NULL when this symbol
1607 was seen in a dynamic object. We must force the union to be
1608 NULL, so that it is correct for a regular symbol. */
1609 h->verinfo.vertree = NULL;
1612 /* Handle the special case of a new common symbol merging with an
1613 old symbol that looks like it might be a common symbol defined in
1614 a shared object. Note that we have already handled the case in
1615 which a new common symbol should simply override the definition
1616 in the shared library. */
1619 && bfd_is_com_section (sec)
1622 /* It would be best if we could set the hash table entry to a
1623 common symbol, but we don't know what to use for the section
1624 or the alignment. */
1625 if (! ((*info->callbacks->multiple_common)
1626 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1629 /* If the presumed common symbol in the dynamic object is
1630 larger, pretend that the new symbol has its size. */
1632 if (h->size > *pvalue)
1635 /* We need to remember the alignment required by the symbol
1636 in the dynamic object. */
1637 BFD_ASSERT (pold_alignment);
1638 *pold_alignment = h->root.u.def.section->alignment_power;
1641 olddyncommon = FALSE;
1643 h->root.type = bfd_link_hash_undefined;
1644 h->root.u.undef.abfd = h->root.u.def.section->owner;
1646 *size_change_ok = TRUE;
1647 *type_change_ok = TRUE;
1649 if (hi->root.type == bfd_link_hash_indirect)
1652 h->verinfo.vertree = NULL;
1657 /* Handle the case where we had a versioned symbol in a dynamic
1658 library and now find a definition in a normal object. In this
1659 case, we make the versioned symbol point to the normal one. */
1660 flip->root.type = h->root.type;
1661 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1662 h->root.type = bfd_link_hash_indirect;
1663 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1664 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1668 flip->ref_dynamic = 1;
1675 /* This function is called to create an indirect symbol from the
1676 default for the symbol with the default version if needed. The
1677 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1678 set DYNSYM if the new indirect symbol is dynamic. */
1681 _bfd_elf_add_default_symbol (bfd *abfd,
1682 struct bfd_link_info *info,
1683 struct elf_link_hash_entry *h,
1685 Elf_Internal_Sym *sym,
1689 bfd_boolean *dynsym)
1691 bfd_boolean type_change_ok;
1692 bfd_boolean size_change_ok;
1695 struct elf_link_hash_entry *hi;
1696 struct bfd_link_hash_entry *bh;
1697 const struct elf_backend_data *bed;
1698 bfd_boolean collect;
1699 bfd_boolean dynamic;
1700 bfd_boolean override;
1702 size_t len, shortlen;
1704 bfd_boolean matched;
1706 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1709 /* If this symbol has a version, and it is the default version, we
1710 create an indirect symbol from the default name to the fully
1711 decorated name. This will cause external references which do not
1712 specify a version to be bound to this version of the symbol. */
1713 p = strchr (name, ELF_VER_CHR);
1714 if (h->versioned == unknown)
1718 h->versioned = unversioned;
1723 if (p[1] != ELF_VER_CHR)
1725 h->versioned = versioned_hidden;
1729 h->versioned = versioned;
1734 /* PR ld/19073: We may see an unversioned definition after the
1740 bed = get_elf_backend_data (abfd);
1741 collect = bed->collect;
1742 dynamic = (abfd->flags & DYNAMIC) != 0;
1744 shortlen = p - name;
1745 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1746 if (shortname == NULL)
1748 memcpy (shortname, name, shortlen);
1749 shortname[shortlen] = '\0';
1751 /* We are going to create a new symbol. Merge it with any existing
1752 symbol with this name. For the purposes of the merge, act as
1753 though we were defining the symbol we just defined, although we
1754 actually going to define an indirect symbol. */
1755 type_change_ok = FALSE;
1756 size_change_ok = FALSE;
1759 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1760 &hi, poldbfd, NULL, NULL, &skip, &override,
1761 &type_change_ok, &size_change_ok, &matched))
1769 /* Add the default symbol if not performing a relocatable link. */
1770 if (! bfd_link_relocatable (info))
1773 if (! (_bfd_generic_link_add_one_symbol
1774 (info, abfd, shortname, BSF_INDIRECT,
1775 bfd_ind_section_ptr,
1776 0, name, FALSE, collect, &bh)))
1778 hi = (struct elf_link_hash_entry *) bh;
1783 /* In this case the symbol named SHORTNAME is overriding the
1784 indirect symbol we want to add. We were planning on making
1785 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1786 is the name without a version. NAME is the fully versioned
1787 name, and it is the default version.
1789 Overriding means that we already saw a definition for the
1790 symbol SHORTNAME in a regular object, and it is overriding
1791 the symbol defined in the dynamic object.
1793 When this happens, we actually want to change NAME, the
1794 symbol we just added, to refer to SHORTNAME. This will cause
1795 references to NAME in the shared object to become references
1796 to SHORTNAME in the regular object. This is what we expect
1797 when we override a function in a shared object: that the
1798 references in the shared object will be mapped to the
1799 definition in the regular object. */
1801 while (hi->root.type == bfd_link_hash_indirect
1802 || hi->root.type == bfd_link_hash_warning)
1803 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1805 h->root.type = bfd_link_hash_indirect;
1806 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1810 hi->ref_dynamic = 1;
1814 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1819 /* Now set HI to H, so that the following code will set the
1820 other fields correctly. */
1824 /* Check if HI is a warning symbol. */
1825 if (hi->root.type == bfd_link_hash_warning)
1826 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1828 /* If there is a duplicate definition somewhere, then HI may not
1829 point to an indirect symbol. We will have reported an error to
1830 the user in that case. */
1832 if (hi->root.type == bfd_link_hash_indirect)
1834 struct elf_link_hash_entry *ht;
1836 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1837 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1839 /* A reference to the SHORTNAME symbol from a dynamic library
1840 will be satisfied by the versioned symbol at runtime. In
1841 effect, we have a reference to the versioned symbol. */
1842 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1843 hi->dynamic_def |= ht->dynamic_def;
1845 /* See if the new flags lead us to realize that the symbol must
1851 if (! bfd_link_executable (info)
1858 if (hi->ref_regular)
1864 /* We also need to define an indirection from the nondefault version
1868 len = strlen (name);
1869 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1870 if (shortname == NULL)
1872 memcpy (shortname, name, shortlen);
1873 memcpy (shortname + shortlen, p + 1, len - shortlen);
1875 /* Once again, merge with any existing symbol. */
1876 type_change_ok = FALSE;
1877 size_change_ok = FALSE;
1879 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1880 &hi, poldbfd, NULL, NULL, &skip, &override,
1881 &type_change_ok, &size_change_ok, &matched))
1889 /* Here SHORTNAME is a versioned name, so we don't expect to see
1890 the type of override we do in the case above unless it is
1891 overridden by a versioned definition. */
1892 if (hi->root.type != bfd_link_hash_defined
1893 && hi->root.type != bfd_link_hash_defweak)
1894 (*_bfd_error_handler)
1895 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1901 if (! (_bfd_generic_link_add_one_symbol
1902 (info, abfd, shortname, BSF_INDIRECT,
1903 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1905 hi = (struct elf_link_hash_entry *) bh;
1907 /* If there is a duplicate definition somewhere, then HI may not
1908 point to an indirect symbol. We will have reported an error
1909 to the user in that case. */
1911 if (hi->root.type == bfd_link_hash_indirect)
1913 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1914 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1915 hi->dynamic_def |= h->dynamic_def;
1917 /* See if the new flags lead us to realize that the symbol
1923 if (! bfd_link_executable (info)
1929 if (hi->ref_regular)
1939 /* This routine is used to export all defined symbols into the dynamic
1940 symbol table. It is called via elf_link_hash_traverse. */
1943 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1945 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1947 /* Ignore indirect symbols. These are added by the versioning code. */
1948 if (h->root.type == bfd_link_hash_indirect)
1951 /* Ignore this if we won't export it. */
1952 if (!eif->info->export_dynamic && !h->dynamic)
1955 if (h->dynindx == -1
1956 && (h->def_regular || h->ref_regular)
1957 && ! bfd_hide_sym_by_version (eif->info->version_info,
1958 h->root.root.string))
1960 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1970 /* Look through the symbols which are defined in other shared
1971 libraries and referenced here. Update the list of version
1972 dependencies. This will be put into the .gnu.version_r section.
1973 This function is called via elf_link_hash_traverse. */
1976 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1979 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1980 Elf_Internal_Verneed *t;
1981 Elf_Internal_Vernaux *a;
1984 /* We only care about symbols defined in shared objects with version
1989 || h->verinfo.verdef == NULL
1990 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1991 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
1994 /* See if we already know about this version. */
1995 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1999 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2002 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2003 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2009 /* This is a new version. Add it to tree we are building. */
2014 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2017 rinfo->failed = TRUE;
2021 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2022 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2023 elf_tdata (rinfo->info->output_bfd)->verref = t;
2027 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2030 rinfo->failed = TRUE;
2034 /* Note that we are copying a string pointer here, and testing it
2035 above. If bfd_elf_string_from_elf_section is ever changed to
2036 discard the string data when low in memory, this will have to be
2038 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2040 a->vna_flags = h->verinfo.verdef->vd_flags;
2041 a->vna_nextptr = t->vn_auxptr;
2043 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2046 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2053 /* Figure out appropriate versions for all the symbols. We may not
2054 have the version number script until we have read all of the input
2055 files, so until that point we don't know which symbols should be
2056 local. This function is called via elf_link_hash_traverse. */
2059 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2061 struct elf_info_failed *sinfo;
2062 struct bfd_link_info *info;
2063 const struct elf_backend_data *bed;
2064 struct elf_info_failed eif;
2068 sinfo = (struct elf_info_failed *) data;
2071 /* Fix the symbol flags. */
2074 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2077 sinfo->failed = TRUE;
2081 /* We only need version numbers for symbols defined in regular
2083 if (!h->def_regular)
2086 bed = get_elf_backend_data (info->output_bfd);
2087 p = strchr (h->root.root.string, ELF_VER_CHR);
2088 if (p != NULL && h->verinfo.vertree == NULL)
2090 struct bfd_elf_version_tree *t;
2093 if (*p == ELF_VER_CHR)
2096 /* If there is no version string, we can just return out. */
2100 /* Look for the version. If we find it, it is no longer weak. */
2101 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2103 if (strcmp (t->name, p) == 0)
2107 struct bfd_elf_version_expr *d;
2109 len = p - h->root.root.string;
2110 alc = (char *) bfd_malloc (len);
2113 sinfo->failed = TRUE;
2116 memcpy (alc, h->root.root.string, len - 1);
2117 alc[len - 1] = '\0';
2118 if (alc[len - 2] == ELF_VER_CHR)
2119 alc[len - 2] = '\0';
2121 h->verinfo.vertree = t;
2125 if (t->globals.list != NULL)
2126 d = (*t->match) (&t->globals, NULL, alc);
2128 /* See if there is anything to force this symbol to
2130 if (d == NULL && t->locals.list != NULL)
2132 d = (*t->match) (&t->locals, NULL, alc);
2135 && ! info->export_dynamic)
2136 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2144 /* If we are building an application, we need to create a
2145 version node for this version. */
2146 if (t == NULL && bfd_link_executable (info))
2148 struct bfd_elf_version_tree **pp;
2151 /* If we aren't going to export this symbol, we don't need
2152 to worry about it. */
2153 if (h->dynindx == -1)
2157 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2160 sinfo->failed = TRUE;
2165 t->name_indx = (unsigned int) -1;
2169 /* Don't count anonymous version tag. */
2170 if (sinfo->info->version_info != NULL
2171 && sinfo->info->version_info->vernum == 0)
2173 for (pp = &sinfo->info->version_info;
2177 t->vernum = version_index;
2181 h->verinfo.vertree = t;
2185 /* We could not find the version for a symbol when
2186 generating a shared archive. Return an error. */
2187 (*_bfd_error_handler)
2188 (_("%B: version node not found for symbol %s"),
2189 info->output_bfd, h->root.root.string);
2190 bfd_set_error (bfd_error_bad_value);
2191 sinfo->failed = TRUE;
2196 /* If we don't have a version for this symbol, see if we can find
2198 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2203 = bfd_find_version_for_sym (sinfo->info->version_info,
2204 h->root.root.string, &hide);
2205 if (h->verinfo.vertree != NULL && hide)
2206 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2212 /* Read and swap the relocs from the section indicated by SHDR. This
2213 may be either a REL or a RELA section. The relocations are
2214 translated into RELA relocations and stored in INTERNAL_RELOCS,
2215 which should have already been allocated to contain enough space.
2216 The EXTERNAL_RELOCS are a buffer where the external form of the
2217 relocations should be stored.
2219 Returns FALSE if something goes wrong. */
2222 elf_link_read_relocs_from_section (bfd *abfd,
2224 Elf_Internal_Shdr *shdr,
2225 void *external_relocs,
2226 Elf_Internal_Rela *internal_relocs)
2228 const struct elf_backend_data *bed;
2229 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2230 const bfd_byte *erela;
2231 const bfd_byte *erelaend;
2232 Elf_Internal_Rela *irela;
2233 Elf_Internal_Shdr *symtab_hdr;
2236 /* Position ourselves at the start of the section. */
2237 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2240 /* Read the relocations. */
2241 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2244 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2245 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2247 bed = get_elf_backend_data (abfd);
2249 /* Convert the external relocations to the internal format. */
2250 if (shdr->sh_entsize == bed->s->sizeof_rel)
2251 swap_in = bed->s->swap_reloc_in;
2252 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2253 swap_in = bed->s->swap_reloca_in;
2256 bfd_set_error (bfd_error_wrong_format);
2260 erela = (const bfd_byte *) external_relocs;
2261 erelaend = erela + shdr->sh_size;
2262 irela = internal_relocs;
2263 while (erela < erelaend)
2267 (*swap_in) (abfd, erela, irela);
2268 r_symndx = ELF32_R_SYM (irela->r_info);
2269 if (bed->s->arch_size == 64)
2273 if ((size_t) r_symndx >= nsyms)
2275 (*_bfd_error_handler)
2276 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2277 " for offset 0x%lx in section `%A'"),
2279 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2280 bfd_set_error (bfd_error_bad_value);
2284 else if (r_symndx != STN_UNDEF)
2286 (*_bfd_error_handler)
2287 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2288 " when the object file has no symbol table"),
2290 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2291 bfd_set_error (bfd_error_bad_value);
2294 irela += bed->s->int_rels_per_ext_rel;
2295 erela += shdr->sh_entsize;
2301 /* Read and swap the relocs for a section O. They may have been
2302 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2303 not NULL, they are used as buffers to read into. They are known to
2304 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2305 the return value is allocated using either malloc or bfd_alloc,
2306 according to the KEEP_MEMORY argument. If O has two relocation
2307 sections (both REL and RELA relocations), then the REL_HDR
2308 relocations will appear first in INTERNAL_RELOCS, followed by the
2309 RELA_HDR relocations. */
2312 _bfd_elf_link_read_relocs (bfd *abfd,
2314 void *external_relocs,
2315 Elf_Internal_Rela *internal_relocs,
2316 bfd_boolean keep_memory)
2318 void *alloc1 = NULL;
2319 Elf_Internal_Rela *alloc2 = NULL;
2320 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2321 struct bfd_elf_section_data *esdo = elf_section_data (o);
2322 Elf_Internal_Rela *internal_rela_relocs;
2324 if (esdo->relocs != NULL)
2325 return esdo->relocs;
2327 if (o->reloc_count == 0)
2330 if (internal_relocs == NULL)
2334 size = o->reloc_count;
2335 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2337 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2339 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2340 if (internal_relocs == NULL)
2344 if (external_relocs == NULL)
2346 bfd_size_type size = 0;
2349 size += esdo->rel.hdr->sh_size;
2351 size += esdo->rela.hdr->sh_size;
2353 alloc1 = bfd_malloc (size);
2356 external_relocs = alloc1;
2359 internal_rela_relocs = internal_relocs;
2362 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2366 external_relocs = (((bfd_byte *) external_relocs)
2367 + esdo->rel.hdr->sh_size);
2368 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2369 * bed->s->int_rels_per_ext_rel);
2373 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2375 internal_rela_relocs)))
2378 /* Cache the results for next time, if we can. */
2380 esdo->relocs = internal_relocs;
2385 /* Don't free alloc2, since if it was allocated we are passing it
2386 back (under the name of internal_relocs). */
2388 return internal_relocs;
2396 bfd_release (abfd, alloc2);
2403 /* Compute the size of, and allocate space for, REL_HDR which is the
2404 section header for a section containing relocations for O. */
2407 _bfd_elf_link_size_reloc_section (bfd *abfd,
2408 struct bfd_elf_section_reloc_data *reldata)
2410 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2412 /* That allows us to calculate the size of the section. */
2413 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2415 /* The contents field must last into write_object_contents, so we
2416 allocate it with bfd_alloc rather than malloc. Also since we
2417 cannot be sure that the contents will actually be filled in,
2418 we zero the allocated space. */
2419 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2420 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2423 if (reldata->hashes == NULL && reldata->count)
2425 struct elf_link_hash_entry **p;
2427 p = ((struct elf_link_hash_entry **)
2428 bfd_zmalloc (reldata->count * sizeof (*p)));
2432 reldata->hashes = p;
2438 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2439 originated from the section given by INPUT_REL_HDR) to the
2443 _bfd_elf_link_output_relocs (bfd *output_bfd,
2444 asection *input_section,
2445 Elf_Internal_Shdr *input_rel_hdr,
2446 Elf_Internal_Rela *internal_relocs,
2447 struct elf_link_hash_entry **rel_hash
2450 Elf_Internal_Rela *irela;
2451 Elf_Internal_Rela *irelaend;
2453 struct bfd_elf_section_reloc_data *output_reldata;
2454 asection *output_section;
2455 const struct elf_backend_data *bed;
2456 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2457 struct bfd_elf_section_data *esdo;
2459 output_section = input_section->output_section;
2461 bed = get_elf_backend_data (output_bfd);
2462 esdo = elf_section_data (output_section);
2463 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2465 output_reldata = &esdo->rel;
2466 swap_out = bed->s->swap_reloc_out;
2468 else if (esdo->rela.hdr
2469 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2471 output_reldata = &esdo->rela;
2472 swap_out = bed->s->swap_reloca_out;
2476 (*_bfd_error_handler)
2477 (_("%B: relocation size mismatch in %B section %A"),
2478 output_bfd, input_section->owner, input_section);
2479 bfd_set_error (bfd_error_wrong_format);
2483 erel = output_reldata->hdr->contents;
2484 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2485 irela = internal_relocs;
2486 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2487 * bed->s->int_rels_per_ext_rel);
2488 while (irela < irelaend)
2490 (*swap_out) (output_bfd, irela, erel);
2491 irela += bed->s->int_rels_per_ext_rel;
2492 erel += input_rel_hdr->sh_entsize;
2495 /* Bump the counter, so that we know where to add the next set of
2497 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2502 /* Make weak undefined symbols in PIE dynamic. */
2505 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2506 struct elf_link_hash_entry *h)
2508 if (bfd_link_pie (info)
2510 && h->root.type == bfd_link_hash_undefweak)
2511 return bfd_elf_link_record_dynamic_symbol (info, h);
2516 /* Fix up the flags for a symbol. This handles various cases which
2517 can only be fixed after all the input files are seen. This is
2518 currently called by both adjust_dynamic_symbol and
2519 assign_sym_version, which is unnecessary but perhaps more robust in
2520 the face of future changes. */
2523 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2524 struct elf_info_failed *eif)
2526 const struct elf_backend_data *bed;
2528 /* If this symbol was mentioned in a non-ELF file, try to set
2529 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2530 permit a non-ELF file to correctly refer to a symbol defined in
2531 an ELF dynamic object. */
2534 while (h->root.type == bfd_link_hash_indirect)
2535 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2537 if (h->root.type != bfd_link_hash_defined
2538 && h->root.type != bfd_link_hash_defweak)
2541 h->ref_regular_nonweak = 1;
2545 if (h->root.u.def.section->owner != NULL
2546 && (bfd_get_flavour (h->root.u.def.section->owner)
2547 == bfd_target_elf_flavour))
2550 h->ref_regular_nonweak = 1;
2556 if (h->dynindx == -1
2560 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2569 /* Unfortunately, NON_ELF is only correct if the symbol
2570 was first seen in a non-ELF file. Fortunately, if the symbol
2571 was first seen in an ELF file, we're probably OK unless the
2572 symbol was defined in a non-ELF file. Catch that case here.
2573 FIXME: We're still in trouble if the symbol was first seen in
2574 a dynamic object, and then later in a non-ELF regular object. */
2575 if ((h->root.type == bfd_link_hash_defined
2576 || h->root.type == bfd_link_hash_defweak)
2578 && (h->root.u.def.section->owner != NULL
2579 ? (bfd_get_flavour (h->root.u.def.section->owner)
2580 != bfd_target_elf_flavour)
2581 : (bfd_is_abs_section (h->root.u.def.section)
2582 && !h->def_dynamic)))
2586 /* Backend specific symbol fixup. */
2587 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2588 if (bed->elf_backend_fixup_symbol
2589 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2592 /* If this is a final link, and the symbol was defined as a common
2593 symbol in a regular object file, and there was no definition in
2594 any dynamic object, then the linker will have allocated space for
2595 the symbol in a common section but the DEF_REGULAR
2596 flag will not have been set. */
2597 if (h->root.type == bfd_link_hash_defined
2601 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2604 /* If -Bsymbolic was used (which means to bind references to global
2605 symbols to the definition within the shared object), and this
2606 symbol was defined in a regular object, then it actually doesn't
2607 need a PLT entry. Likewise, if the symbol has non-default
2608 visibility. If the symbol has hidden or internal visibility, we
2609 will force it local. */
2611 && bfd_link_pic (eif->info)
2612 && is_elf_hash_table (eif->info->hash)
2613 && (SYMBOLIC_BIND (eif->info, h)
2614 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2617 bfd_boolean force_local;
2619 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2620 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2621 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2624 /* If a weak undefined symbol has non-default visibility, we also
2625 hide it from the dynamic linker. */
2626 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2627 && h->root.type == bfd_link_hash_undefweak)
2628 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2630 /* If this is a weak defined symbol in a dynamic object, and we know
2631 the real definition in the dynamic object, copy interesting flags
2632 over to the real definition. */
2633 if (h->u.weakdef != NULL)
2635 /* If the real definition is defined by a regular object file,
2636 don't do anything special. See the longer description in
2637 _bfd_elf_adjust_dynamic_symbol, below. */
2638 if (h->u.weakdef->def_regular)
2639 h->u.weakdef = NULL;
2642 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2644 while (h->root.type == bfd_link_hash_indirect)
2645 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2647 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2648 || h->root.type == bfd_link_hash_defweak);
2649 BFD_ASSERT (weakdef->def_dynamic);
2650 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2651 || weakdef->root.type == bfd_link_hash_defweak);
2652 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2659 /* Make the backend pick a good value for a dynamic symbol. This is
2660 called via elf_link_hash_traverse, and also calls itself
2664 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2666 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2668 const struct elf_backend_data *bed;
2670 if (! is_elf_hash_table (eif->info->hash))
2673 /* Ignore indirect symbols. These are added by the versioning code. */
2674 if (h->root.type == bfd_link_hash_indirect)
2677 /* Fix the symbol flags. */
2678 if (! _bfd_elf_fix_symbol_flags (h, eif))
2681 /* If this symbol does not require a PLT entry, and it is not
2682 defined by a dynamic object, or is not referenced by a regular
2683 object, ignore it. We do have to handle a weak defined symbol,
2684 even if no regular object refers to it, if we decided to add it
2685 to the dynamic symbol table. FIXME: Do we normally need to worry
2686 about symbols which are defined by one dynamic object and
2687 referenced by another one? */
2689 && h->type != STT_GNU_IFUNC
2693 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2695 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2699 /* If we've already adjusted this symbol, don't do it again. This
2700 can happen via a recursive call. */
2701 if (h->dynamic_adjusted)
2704 /* Don't look at this symbol again. Note that we must set this
2705 after checking the above conditions, because we may look at a
2706 symbol once, decide not to do anything, and then get called
2707 recursively later after REF_REGULAR is set below. */
2708 h->dynamic_adjusted = 1;
2710 /* If this is a weak definition, and we know a real definition, and
2711 the real symbol is not itself defined by a regular object file,
2712 then get a good value for the real definition. We handle the
2713 real symbol first, for the convenience of the backend routine.
2715 Note that there is a confusing case here. If the real definition
2716 is defined by a regular object file, we don't get the real symbol
2717 from the dynamic object, but we do get the weak symbol. If the
2718 processor backend uses a COPY reloc, then if some routine in the
2719 dynamic object changes the real symbol, we will not see that
2720 change in the corresponding weak symbol. This is the way other
2721 ELF linkers work as well, and seems to be a result of the shared
2724 I will clarify this issue. Most SVR4 shared libraries define the
2725 variable _timezone and define timezone as a weak synonym. The
2726 tzset call changes _timezone. If you write
2727 extern int timezone;
2729 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2730 you might expect that, since timezone is a synonym for _timezone,
2731 the same number will print both times. However, if the processor
2732 backend uses a COPY reloc, then actually timezone will be copied
2733 into your process image, and, since you define _timezone
2734 yourself, _timezone will not. Thus timezone and _timezone will
2735 wind up at different memory locations. The tzset call will set
2736 _timezone, leaving timezone unchanged. */
2738 if (h->u.weakdef != NULL)
2740 /* If we get to this point, there is an implicit reference to
2741 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2742 h->u.weakdef->ref_regular = 1;
2744 /* Ensure that the backend adjust_dynamic_symbol function sees
2745 H->U.WEAKDEF before H by recursively calling ourselves. */
2746 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2750 /* If a symbol has no type and no size and does not require a PLT
2751 entry, then we are probably about to do the wrong thing here: we
2752 are probably going to create a COPY reloc for an empty object.
2753 This case can arise when a shared object is built with assembly
2754 code, and the assembly code fails to set the symbol type. */
2756 && h->type == STT_NOTYPE
2758 (*_bfd_error_handler)
2759 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2760 h->root.root.string);
2762 dynobj = elf_hash_table (eif->info)->dynobj;
2763 bed = get_elf_backend_data (dynobj);
2765 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2774 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2778 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2779 struct elf_link_hash_entry *h,
2782 unsigned int power_of_two;
2784 asection *sec = h->root.u.def.section;
2786 /* The section aligment of definition is the maximum alignment
2787 requirement of symbols defined in the section. Since we don't
2788 know the symbol alignment requirement, we start with the
2789 maximum alignment and check low bits of the symbol address
2790 for the minimum alignment. */
2791 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2792 mask = ((bfd_vma) 1 << power_of_two) - 1;
2793 while ((h->root.u.def.value & mask) != 0)
2799 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2802 /* Adjust the section alignment if needed. */
2803 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2808 /* We make sure that the symbol will be aligned properly. */
2809 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2811 /* Define the symbol as being at this point in DYNBSS. */
2812 h->root.u.def.section = dynbss;
2813 h->root.u.def.value = dynbss->size;
2815 /* Increment the size of DYNBSS to make room for the symbol. */
2816 dynbss->size += h->size;
2818 /* No error if extern_protected_data is true. */
2819 if (h->protected_def
2820 && (!info->extern_protected_data
2821 || (info->extern_protected_data < 0
2822 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2823 info->callbacks->einfo
2824 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2825 h->root.root.string);
2830 /* Adjust all external symbols pointing into SEC_MERGE sections
2831 to reflect the object merging within the sections. */
2834 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2838 if ((h->root.type == bfd_link_hash_defined
2839 || h->root.type == bfd_link_hash_defweak)
2840 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2841 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2843 bfd *output_bfd = (bfd *) data;
2845 h->root.u.def.value =
2846 _bfd_merged_section_offset (output_bfd,
2847 &h->root.u.def.section,
2848 elf_section_data (sec)->sec_info,
2849 h->root.u.def.value);
2855 /* Returns false if the symbol referred to by H should be considered
2856 to resolve local to the current module, and true if it should be
2857 considered to bind dynamically. */
2860 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2861 struct bfd_link_info *info,
2862 bfd_boolean not_local_protected)
2864 bfd_boolean binding_stays_local_p;
2865 const struct elf_backend_data *bed;
2866 struct elf_link_hash_table *hash_table;
2871 while (h->root.type == bfd_link_hash_indirect
2872 || h->root.type == bfd_link_hash_warning)
2873 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2875 /* If it was forced local, then clearly it's not dynamic. */
2876 if (h->dynindx == -1)
2878 if (h->forced_local)
2881 /* Identify the cases where name binding rules say that a
2882 visible symbol resolves locally. */
2883 binding_stays_local_p = (bfd_link_executable (info)
2884 || SYMBOLIC_BIND (info, h));
2886 switch (ELF_ST_VISIBILITY (h->other))
2893 hash_table = elf_hash_table (info);
2894 if (!is_elf_hash_table (hash_table))
2897 bed = get_elf_backend_data (hash_table->dynobj);
2899 /* Proper resolution for function pointer equality may require
2900 that these symbols perhaps be resolved dynamically, even though
2901 we should be resolving them to the current module. */
2902 if (!not_local_protected || !bed->is_function_type (h->type))
2903 binding_stays_local_p = TRUE;
2910 /* If it isn't defined locally, then clearly it's dynamic. */
2911 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2914 /* Otherwise, the symbol is dynamic if binding rules don't tell
2915 us that it remains local. */
2916 return !binding_stays_local_p;
2919 /* Return true if the symbol referred to by H should be considered
2920 to resolve local to the current module, and false otherwise. Differs
2921 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2922 undefined symbols. The two functions are virtually identical except
2923 for the place where forced_local and dynindx == -1 are tested. If
2924 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2925 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2926 the symbol is local only for defined symbols.
2927 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2928 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2929 treatment of undefined weak symbols. For those that do not make
2930 undefined weak symbols dynamic, both functions may return false. */
2933 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2934 struct bfd_link_info *info,
2935 bfd_boolean local_protected)
2937 const struct elf_backend_data *bed;
2938 struct elf_link_hash_table *hash_table;
2940 /* If it's a local sym, of course we resolve locally. */
2944 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2945 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2946 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2949 /* Common symbols that become definitions don't get the DEF_REGULAR
2950 flag set, so test it first, and don't bail out. */
2951 if (ELF_COMMON_DEF_P (h))
2953 /* If we don't have a definition in a regular file, then we can't
2954 resolve locally. The sym is either undefined or dynamic. */
2955 else if (!h->def_regular)
2958 /* Forced local symbols resolve locally. */
2959 if (h->forced_local)
2962 /* As do non-dynamic symbols. */
2963 if (h->dynindx == -1)
2966 /* At this point, we know the symbol is defined and dynamic. In an
2967 executable it must resolve locally, likewise when building symbolic
2968 shared libraries. */
2969 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
2972 /* Now deal with defined dynamic symbols in shared libraries. Ones
2973 with default visibility might not resolve locally. */
2974 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2977 hash_table = elf_hash_table (info);
2978 if (!is_elf_hash_table (hash_table))
2981 bed = get_elf_backend_data (hash_table->dynobj);
2983 /* If extern_protected_data is false, STV_PROTECTED non-function
2984 symbols are local. */
2985 if ((!info->extern_protected_data
2986 || (info->extern_protected_data < 0
2987 && !bed->extern_protected_data))
2988 && !bed->is_function_type (h->type))
2991 /* Function pointer equality tests may require that STV_PROTECTED
2992 symbols be treated as dynamic symbols. If the address of a
2993 function not defined in an executable is set to that function's
2994 plt entry in the executable, then the address of the function in
2995 a shared library must also be the plt entry in the executable. */
2996 return local_protected;
2999 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3000 aligned. Returns the first TLS output section. */
3002 struct bfd_section *
3003 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3005 struct bfd_section *sec, *tls;
3006 unsigned int align = 0;
3008 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3009 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3013 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3014 if (sec->alignment_power > align)
3015 align = sec->alignment_power;
3017 elf_hash_table (info)->tls_sec = tls;
3019 /* Ensure the alignment of the first section is the largest alignment,
3020 so that the tls segment starts aligned. */
3022 tls->alignment_power = align;
3027 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3029 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3030 Elf_Internal_Sym *sym)
3032 const struct elf_backend_data *bed;
3034 /* Local symbols do not count, but target specific ones might. */
3035 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3036 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3039 bed = get_elf_backend_data (abfd);
3040 /* Function symbols do not count. */
3041 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3044 /* If the section is undefined, then so is the symbol. */
3045 if (sym->st_shndx == SHN_UNDEF)
3048 /* If the symbol is defined in the common section, then
3049 it is a common definition and so does not count. */
3050 if (bed->common_definition (sym))
3053 /* If the symbol is in a target specific section then we
3054 must rely upon the backend to tell us what it is. */
3055 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3056 /* FIXME - this function is not coded yet:
3058 return _bfd_is_global_symbol_definition (abfd, sym);
3060 Instead for now assume that the definition is not global,
3061 Even if this is wrong, at least the linker will behave
3062 in the same way that it used to do. */
3068 /* Search the symbol table of the archive element of the archive ABFD
3069 whose archive map contains a mention of SYMDEF, and determine if
3070 the symbol is defined in this element. */
3072 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3074 Elf_Internal_Shdr * hdr;
3075 bfd_size_type symcount;
3076 bfd_size_type extsymcount;
3077 bfd_size_type extsymoff;
3078 Elf_Internal_Sym *isymbuf;
3079 Elf_Internal_Sym *isym;
3080 Elf_Internal_Sym *isymend;
3083 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3087 /* Return FALSE if the object has been claimed by plugin. */
3088 if (abfd->plugin_format == bfd_plugin_yes)
3091 if (! bfd_check_format (abfd, bfd_object))
3094 /* Select the appropriate symbol table. */
3095 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3096 hdr = &elf_tdata (abfd)->symtab_hdr;
3098 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3100 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3102 /* The sh_info field of the symtab header tells us where the
3103 external symbols start. We don't care about the local symbols. */
3104 if (elf_bad_symtab (abfd))
3106 extsymcount = symcount;
3111 extsymcount = symcount - hdr->sh_info;
3112 extsymoff = hdr->sh_info;
3115 if (extsymcount == 0)
3118 /* Read in the symbol table. */
3119 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3121 if (isymbuf == NULL)
3124 /* Scan the symbol table looking for SYMDEF. */
3126 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3130 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3135 if (strcmp (name, symdef->name) == 0)
3137 result = is_global_data_symbol_definition (abfd, isym);
3147 /* Add an entry to the .dynamic table. */
3150 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3154 struct elf_link_hash_table *hash_table;
3155 const struct elf_backend_data *bed;
3157 bfd_size_type newsize;
3158 bfd_byte *newcontents;
3159 Elf_Internal_Dyn dyn;
3161 hash_table = elf_hash_table (info);
3162 if (! is_elf_hash_table (hash_table))
3165 bed = get_elf_backend_data (hash_table->dynobj);
3166 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3167 BFD_ASSERT (s != NULL);
3169 newsize = s->size + bed->s->sizeof_dyn;
3170 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3171 if (newcontents == NULL)
3175 dyn.d_un.d_val = val;
3176 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3179 s->contents = newcontents;
3184 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3185 otherwise just check whether one already exists. Returns -1 on error,
3186 1 if a DT_NEEDED tag already exists, and 0 on success. */
3189 elf_add_dt_needed_tag (bfd *abfd,
3190 struct bfd_link_info *info,
3194 struct elf_link_hash_table *hash_table;
3195 bfd_size_type strindex;
3197 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3200 hash_table = elf_hash_table (info);
3201 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3202 if (strindex == (bfd_size_type) -1)
3205 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3208 const struct elf_backend_data *bed;
3211 bed = get_elf_backend_data (hash_table->dynobj);
3212 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3214 for (extdyn = sdyn->contents;
3215 extdyn < sdyn->contents + sdyn->size;
3216 extdyn += bed->s->sizeof_dyn)
3218 Elf_Internal_Dyn dyn;
3220 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3221 if (dyn.d_tag == DT_NEEDED
3222 && dyn.d_un.d_val == strindex)
3224 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3232 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3235 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3239 /* We were just checking for existence of the tag. */
3240 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3246 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3248 for (; needed != NULL; needed = needed->next)
3249 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3250 && strcmp (soname, needed->name) == 0)
3256 /* Sort symbol by value, section, and size. */
3258 elf_sort_symbol (const void *arg1, const void *arg2)
3260 const struct elf_link_hash_entry *h1;
3261 const struct elf_link_hash_entry *h2;
3262 bfd_signed_vma vdiff;
3264 h1 = *(const struct elf_link_hash_entry **) arg1;
3265 h2 = *(const struct elf_link_hash_entry **) arg2;
3266 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3268 return vdiff > 0 ? 1 : -1;
3271 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3273 return sdiff > 0 ? 1 : -1;
3275 vdiff = h1->size - h2->size;
3276 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3279 /* This function is used to adjust offsets into .dynstr for
3280 dynamic symbols. This is called via elf_link_hash_traverse. */
3283 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3285 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3287 if (h->dynindx != -1)
3288 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3292 /* Assign string offsets in .dynstr, update all structures referencing
3296 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3298 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3299 struct elf_link_local_dynamic_entry *entry;
3300 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3301 bfd *dynobj = hash_table->dynobj;
3304 const struct elf_backend_data *bed;
3307 _bfd_elf_strtab_finalize (dynstr);
3308 size = _bfd_elf_strtab_size (dynstr);
3310 bed = get_elf_backend_data (dynobj);
3311 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3312 BFD_ASSERT (sdyn != NULL);
3314 /* Update all .dynamic entries referencing .dynstr strings. */
3315 for (extdyn = sdyn->contents;
3316 extdyn < sdyn->contents + sdyn->size;
3317 extdyn += bed->s->sizeof_dyn)
3319 Elf_Internal_Dyn dyn;
3321 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3325 dyn.d_un.d_val = size;
3335 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3340 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3343 /* Now update local dynamic symbols. */
3344 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3345 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3346 entry->isym.st_name);
3348 /* And the rest of dynamic symbols. */
3349 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3351 /* Adjust version definitions. */
3352 if (elf_tdata (output_bfd)->cverdefs)
3357 Elf_Internal_Verdef def;
3358 Elf_Internal_Verdaux defaux;
3360 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3364 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3366 p += sizeof (Elf_External_Verdef);
3367 if (def.vd_aux != sizeof (Elf_External_Verdef))
3369 for (i = 0; i < def.vd_cnt; ++i)
3371 _bfd_elf_swap_verdaux_in (output_bfd,
3372 (Elf_External_Verdaux *) p, &defaux);
3373 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3375 _bfd_elf_swap_verdaux_out (output_bfd,
3376 &defaux, (Elf_External_Verdaux *) p);
3377 p += sizeof (Elf_External_Verdaux);
3380 while (def.vd_next);
3383 /* Adjust version references. */
3384 if (elf_tdata (output_bfd)->verref)
3389 Elf_Internal_Verneed need;
3390 Elf_Internal_Vernaux needaux;
3392 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3396 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3398 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3399 _bfd_elf_swap_verneed_out (output_bfd, &need,
3400 (Elf_External_Verneed *) p);
3401 p += sizeof (Elf_External_Verneed);
3402 for (i = 0; i < need.vn_cnt; ++i)
3404 _bfd_elf_swap_vernaux_in (output_bfd,
3405 (Elf_External_Vernaux *) p, &needaux);
3406 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3408 _bfd_elf_swap_vernaux_out (output_bfd,
3410 (Elf_External_Vernaux *) p);
3411 p += sizeof (Elf_External_Vernaux);
3414 while (need.vn_next);
3420 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3421 The default is to only match when the INPUT and OUTPUT are exactly
3425 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3426 const bfd_target *output)
3428 return input == output;
3431 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3432 This version is used when different targets for the same architecture
3433 are virtually identical. */
3436 _bfd_elf_relocs_compatible (const bfd_target *input,
3437 const bfd_target *output)
3439 const struct elf_backend_data *obed, *ibed;
3441 if (input == output)
3444 ibed = xvec_get_elf_backend_data (input);
3445 obed = xvec_get_elf_backend_data (output);
3447 if (ibed->arch != obed->arch)
3450 /* If both backends are using this function, deem them compatible. */
3451 return ibed->relocs_compatible == obed->relocs_compatible;
3454 /* Make a special call to the linker "notice" function to tell it that
3455 we are about to handle an as-needed lib, or have finished
3456 processing the lib. */
3459 _bfd_elf_notice_as_needed (bfd *ibfd,
3460 struct bfd_link_info *info,
3461 enum notice_asneeded_action act)
3463 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3466 /* Add symbols from an ELF object file to the linker hash table. */
3469 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3471 Elf_Internal_Ehdr *ehdr;
3472 Elf_Internal_Shdr *hdr;
3473 bfd_size_type symcount;
3474 bfd_size_type extsymcount;
3475 bfd_size_type extsymoff;
3476 struct elf_link_hash_entry **sym_hash;
3477 bfd_boolean dynamic;
3478 Elf_External_Versym *extversym = NULL;
3479 Elf_External_Versym *ever;
3480 struct elf_link_hash_entry *weaks;
3481 struct elf_link_hash_entry **nondeflt_vers = NULL;
3482 bfd_size_type nondeflt_vers_cnt = 0;
3483 Elf_Internal_Sym *isymbuf = NULL;
3484 Elf_Internal_Sym *isym;
3485 Elf_Internal_Sym *isymend;
3486 const struct elf_backend_data *bed;
3487 bfd_boolean add_needed;
3488 struct elf_link_hash_table *htab;
3490 void *alloc_mark = NULL;
3491 struct bfd_hash_entry **old_table = NULL;
3492 unsigned int old_size = 0;
3493 unsigned int old_count = 0;
3494 void *old_tab = NULL;
3496 struct bfd_link_hash_entry *old_undefs = NULL;
3497 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3498 long old_dynsymcount = 0;
3499 bfd_size_type old_dynstr_size = 0;
3502 bfd_boolean just_syms;
3504 htab = elf_hash_table (info);
3505 bed = get_elf_backend_data (abfd);
3507 if ((abfd->flags & DYNAMIC) == 0)
3513 /* You can't use -r against a dynamic object. Also, there's no
3514 hope of using a dynamic object which does not exactly match
3515 the format of the output file. */
3516 if (bfd_link_relocatable (info)
3517 || !is_elf_hash_table (htab)
3518 || info->output_bfd->xvec != abfd->xvec)
3520 if (bfd_link_relocatable (info))
3521 bfd_set_error (bfd_error_invalid_operation);
3523 bfd_set_error (bfd_error_wrong_format);
3528 ehdr = elf_elfheader (abfd);
3529 if (info->warn_alternate_em
3530 && bed->elf_machine_code != ehdr->e_machine
3531 && ((bed->elf_machine_alt1 != 0
3532 && ehdr->e_machine == bed->elf_machine_alt1)
3533 || (bed->elf_machine_alt2 != 0
3534 && ehdr->e_machine == bed->elf_machine_alt2)))
3535 info->callbacks->einfo
3536 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3537 ehdr->e_machine, abfd, bed->elf_machine_code);
3539 /* As a GNU extension, any input sections which are named
3540 .gnu.warning.SYMBOL are treated as warning symbols for the given
3541 symbol. This differs from .gnu.warning sections, which generate
3542 warnings when they are included in an output file. */
3543 /* PR 12761: Also generate this warning when building shared libraries. */
3544 for (s = abfd->sections; s != NULL; s = s->next)
3548 name = bfd_get_section_name (abfd, s);
3549 if (CONST_STRNEQ (name, ".gnu.warning."))
3554 name += sizeof ".gnu.warning." - 1;
3556 /* If this is a shared object, then look up the symbol
3557 in the hash table. If it is there, and it is already
3558 been defined, then we will not be using the entry
3559 from this shared object, so we don't need to warn.
3560 FIXME: If we see the definition in a regular object
3561 later on, we will warn, but we shouldn't. The only
3562 fix is to keep track of what warnings we are supposed
3563 to emit, and then handle them all at the end of the
3567 struct elf_link_hash_entry *h;
3569 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3571 /* FIXME: What about bfd_link_hash_common? */
3573 && (h->root.type == bfd_link_hash_defined
3574 || h->root.type == bfd_link_hash_defweak))
3579 msg = (char *) bfd_alloc (abfd, sz + 1);
3583 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3588 if (! (_bfd_generic_link_add_one_symbol
3589 (info, abfd, name, BSF_WARNING, s, 0, msg,
3590 FALSE, bed->collect, NULL)))
3593 if (bfd_link_executable (info))
3595 /* Clobber the section size so that the warning does
3596 not get copied into the output file. */
3599 /* Also set SEC_EXCLUDE, so that symbols defined in
3600 the warning section don't get copied to the output. */
3601 s->flags |= SEC_EXCLUDE;
3606 just_syms = ((s = abfd->sections) != NULL
3607 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3612 /* If we are creating a shared library, create all the dynamic
3613 sections immediately. We need to attach them to something,
3614 so we attach them to this BFD, provided it is the right
3615 format and is not from ld --just-symbols. Always create the
3616 dynamic sections for -E/--dynamic-list. FIXME: If there
3617 are no input BFD's of the same format as the output, we can't
3618 make a shared library. */
3620 && (bfd_link_pic (info)
3621 || info->export_dynamic
3623 && is_elf_hash_table (htab)
3624 && info->output_bfd->xvec == abfd->xvec
3625 && !htab->dynamic_sections_created)
3627 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3631 else if (!is_elf_hash_table (htab))
3635 const char *soname = NULL;
3637 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3640 /* ld --just-symbols and dynamic objects don't mix very well.
3641 ld shouldn't allow it. */
3645 /* If this dynamic lib was specified on the command line with
3646 --as-needed in effect, then we don't want to add a DT_NEEDED
3647 tag unless the lib is actually used. Similary for libs brought
3648 in by another lib's DT_NEEDED. When --no-add-needed is used
3649 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3650 any dynamic library in DT_NEEDED tags in the dynamic lib at
3652 add_needed = (elf_dyn_lib_class (abfd)
3653 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3654 | DYN_NO_NEEDED)) == 0;
3656 s = bfd_get_section_by_name (abfd, ".dynamic");
3661 unsigned int elfsec;
3662 unsigned long shlink;
3664 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3671 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3672 if (elfsec == SHN_BAD)
3673 goto error_free_dyn;
3674 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3676 for (extdyn = dynbuf;
3677 extdyn < dynbuf + s->size;
3678 extdyn += bed->s->sizeof_dyn)
3680 Elf_Internal_Dyn dyn;
3682 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3683 if (dyn.d_tag == DT_SONAME)
3685 unsigned int tagv = dyn.d_un.d_val;
3686 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3688 goto error_free_dyn;
3690 if (dyn.d_tag == DT_NEEDED)
3692 struct bfd_link_needed_list *n, **pn;
3694 unsigned int tagv = dyn.d_un.d_val;
3696 amt = sizeof (struct bfd_link_needed_list);
3697 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3698 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3699 if (n == NULL || fnm == NULL)
3700 goto error_free_dyn;
3701 amt = strlen (fnm) + 1;
3702 anm = (char *) bfd_alloc (abfd, amt);
3704 goto error_free_dyn;
3705 memcpy (anm, fnm, amt);
3709 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3713 if (dyn.d_tag == DT_RUNPATH)
3715 struct bfd_link_needed_list *n, **pn;
3717 unsigned int tagv = dyn.d_un.d_val;
3719 amt = sizeof (struct bfd_link_needed_list);
3720 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3721 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3722 if (n == NULL || fnm == NULL)
3723 goto error_free_dyn;
3724 amt = strlen (fnm) + 1;
3725 anm = (char *) bfd_alloc (abfd, amt);
3727 goto error_free_dyn;
3728 memcpy (anm, fnm, amt);
3732 for (pn = & runpath;
3738 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3739 if (!runpath && dyn.d_tag == DT_RPATH)
3741 struct bfd_link_needed_list *n, **pn;
3743 unsigned int tagv = dyn.d_un.d_val;
3745 amt = sizeof (struct bfd_link_needed_list);
3746 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3747 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3748 if (n == NULL || fnm == NULL)
3749 goto error_free_dyn;
3750 amt = strlen (fnm) + 1;
3751 anm = (char *) bfd_alloc (abfd, amt);
3753 goto error_free_dyn;
3754 memcpy (anm, fnm, amt);
3764 if (dyn.d_tag == DT_AUDIT)
3766 unsigned int tagv = dyn.d_un.d_val;
3767 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3774 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3775 frees all more recently bfd_alloc'd blocks as well. */
3781 struct bfd_link_needed_list **pn;
3782 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3787 /* We do not want to include any of the sections in a dynamic
3788 object in the output file. We hack by simply clobbering the
3789 list of sections in the BFD. This could be handled more
3790 cleanly by, say, a new section flag; the existing
3791 SEC_NEVER_LOAD flag is not the one we want, because that one
3792 still implies that the section takes up space in the output
3794 bfd_section_list_clear (abfd);
3796 /* Find the name to use in a DT_NEEDED entry that refers to this
3797 object. If the object has a DT_SONAME entry, we use it.
3798 Otherwise, if the generic linker stuck something in
3799 elf_dt_name, we use that. Otherwise, we just use the file
3801 if (soname == NULL || *soname == '\0')
3803 soname = elf_dt_name (abfd);
3804 if (soname == NULL || *soname == '\0')
3805 soname = bfd_get_filename (abfd);
3808 /* Save the SONAME because sometimes the linker emulation code
3809 will need to know it. */
3810 elf_dt_name (abfd) = soname;
3812 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3816 /* If we have already included this dynamic object in the
3817 link, just ignore it. There is no reason to include a
3818 particular dynamic object more than once. */
3822 /* Save the DT_AUDIT entry for the linker emulation code. */
3823 elf_dt_audit (abfd) = audit;
3826 /* If this is a dynamic object, we always link against the .dynsym
3827 symbol table, not the .symtab symbol table. The dynamic linker
3828 will only see the .dynsym symbol table, so there is no reason to
3829 look at .symtab for a dynamic object. */
3831 if (! dynamic || elf_dynsymtab (abfd) == 0)
3832 hdr = &elf_tdata (abfd)->symtab_hdr;
3834 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3836 symcount = hdr->sh_size / bed->s->sizeof_sym;
3838 /* The sh_info field of the symtab header tells us where the
3839 external symbols start. We don't care about the local symbols at
3841 if (elf_bad_symtab (abfd))
3843 extsymcount = symcount;
3848 extsymcount = symcount - hdr->sh_info;
3849 extsymoff = hdr->sh_info;
3852 sym_hash = elf_sym_hashes (abfd);
3853 if (extsymcount != 0)
3855 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3857 if (isymbuf == NULL)
3860 if (sym_hash == NULL)
3862 /* We store a pointer to the hash table entry for each
3864 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3865 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3866 if (sym_hash == NULL)
3867 goto error_free_sym;
3868 elf_sym_hashes (abfd) = sym_hash;
3874 /* Read in any version definitions. */
3875 if (!_bfd_elf_slurp_version_tables (abfd,
3876 info->default_imported_symver))
3877 goto error_free_sym;
3879 /* Read in the symbol versions, but don't bother to convert them
3880 to internal format. */
3881 if (elf_dynversym (abfd) != 0)
3883 Elf_Internal_Shdr *versymhdr;
3885 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3886 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3887 if (extversym == NULL)
3888 goto error_free_sym;
3889 amt = versymhdr->sh_size;
3890 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3891 || bfd_bread (extversym, amt, abfd) != amt)
3892 goto error_free_vers;
3896 /* If we are loading an as-needed shared lib, save the symbol table
3897 state before we start adding symbols. If the lib turns out
3898 to be unneeded, restore the state. */
3899 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3904 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3906 struct bfd_hash_entry *p;
3907 struct elf_link_hash_entry *h;
3909 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3911 h = (struct elf_link_hash_entry *) p;
3912 entsize += htab->root.table.entsize;
3913 if (h->root.type == bfd_link_hash_warning)
3914 entsize += htab->root.table.entsize;
3918 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3919 old_tab = bfd_malloc (tabsize + entsize);
3920 if (old_tab == NULL)
3921 goto error_free_vers;
3923 /* Remember the current objalloc pointer, so that all mem for
3924 symbols added can later be reclaimed. */
3925 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3926 if (alloc_mark == NULL)
3927 goto error_free_vers;
3929 /* Make a special call to the linker "notice" function to
3930 tell it that we are about to handle an as-needed lib. */
3931 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
3932 goto error_free_vers;
3934 /* Clone the symbol table. Remember some pointers into the
3935 symbol table, and dynamic symbol count. */
3936 old_ent = (char *) old_tab + tabsize;
3937 memcpy (old_tab, htab->root.table.table, tabsize);
3938 old_undefs = htab->root.undefs;
3939 old_undefs_tail = htab->root.undefs_tail;
3940 old_table = htab->root.table.table;
3941 old_size = htab->root.table.size;
3942 old_count = htab->root.table.count;
3943 old_dynsymcount = htab->dynsymcount;
3944 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
3946 for (i = 0; i < htab->root.table.size; i++)
3948 struct bfd_hash_entry *p;
3949 struct elf_link_hash_entry *h;
3951 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3953 memcpy (old_ent, p, htab->root.table.entsize);
3954 old_ent = (char *) old_ent + htab->root.table.entsize;
3955 h = (struct elf_link_hash_entry *) p;
3956 if (h->root.type == bfd_link_hash_warning)
3958 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3959 old_ent = (char *) old_ent + htab->root.table.entsize;
3966 ever = extversym != NULL ? extversym + extsymoff : NULL;
3967 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3969 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3973 asection *sec, *new_sec;
3976 struct elf_link_hash_entry *h;
3977 struct elf_link_hash_entry *hi;
3978 bfd_boolean definition;
3979 bfd_boolean size_change_ok;
3980 bfd_boolean type_change_ok;
3981 bfd_boolean new_weakdef;
3982 bfd_boolean new_weak;
3983 bfd_boolean old_weak;
3984 bfd_boolean override;
3986 unsigned int old_alignment;
3988 bfd_boolean matched;
3992 flags = BSF_NO_FLAGS;
3994 value = isym->st_value;
3995 common = bed->common_definition (isym);
3997 bind = ELF_ST_BIND (isym->st_info);
4001 /* This should be impossible, since ELF requires that all
4002 global symbols follow all local symbols, and that sh_info
4003 point to the first global symbol. Unfortunately, Irix 5
4008 if (isym->st_shndx != SHN_UNDEF && !common)
4016 case STB_GNU_UNIQUE:
4017 flags = BSF_GNU_UNIQUE;
4021 /* Leave it up to the processor backend. */
4025 if (isym->st_shndx == SHN_UNDEF)
4026 sec = bfd_und_section_ptr;
4027 else if (isym->st_shndx == SHN_ABS)
4028 sec = bfd_abs_section_ptr;
4029 else if (isym->st_shndx == SHN_COMMON)
4031 sec = bfd_com_section_ptr;
4032 /* What ELF calls the size we call the value. What ELF
4033 calls the value we call the alignment. */
4034 value = isym->st_size;
4038 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4040 sec = bfd_abs_section_ptr;
4041 else if (discarded_section (sec))
4043 /* Symbols from discarded section are undefined. We keep
4045 sec = bfd_und_section_ptr;
4046 isym->st_shndx = SHN_UNDEF;
4048 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4052 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4055 goto error_free_vers;
4057 if (isym->st_shndx == SHN_COMMON
4058 && (abfd->flags & BFD_PLUGIN) != 0)
4060 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4064 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4066 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4068 goto error_free_vers;
4072 else if (isym->st_shndx == SHN_COMMON
4073 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4074 && !bfd_link_relocatable (info))
4076 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4080 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4081 | SEC_LINKER_CREATED);
4082 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4084 goto error_free_vers;
4088 else if (bed->elf_add_symbol_hook)
4090 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4092 goto error_free_vers;
4094 /* The hook function sets the name to NULL if this symbol
4095 should be skipped for some reason. */
4100 /* Sanity check that all possibilities were handled. */
4103 bfd_set_error (bfd_error_bad_value);
4104 goto error_free_vers;
4107 /* Silently discard TLS symbols from --just-syms. There's
4108 no way to combine a static TLS block with a new TLS block
4109 for this executable. */
4110 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4111 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4114 if (bfd_is_und_section (sec)
4115 || bfd_is_com_section (sec))
4120 size_change_ok = FALSE;
4121 type_change_ok = bed->type_change_ok;
4128 if (is_elf_hash_table (htab))
4130 Elf_Internal_Versym iver;
4131 unsigned int vernum = 0;
4136 if (info->default_imported_symver)
4137 /* Use the default symbol version created earlier. */
4138 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4143 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4145 vernum = iver.vs_vers & VERSYM_VERSION;
4147 /* If this is a hidden symbol, or if it is not version
4148 1, we append the version name to the symbol name.
4149 However, we do not modify a non-hidden absolute symbol
4150 if it is not a function, because it might be the version
4151 symbol itself. FIXME: What if it isn't? */
4152 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4154 && (!bfd_is_abs_section (sec)
4155 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4158 size_t namelen, verlen, newlen;
4161 if (isym->st_shndx != SHN_UNDEF)
4163 if (vernum > elf_tdata (abfd)->cverdefs)
4165 else if (vernum > 1)
4167 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4173 (*_bfd_error_handler)
4174 (_("%B: %s: invalid version %u (max %d)"),
4176 elf_tdata (abfd)->cverdefs);
4177 bfd_set_error (bfd_error_bad_value);
4178 goto error_free_vers;
4183 /* We cannot simply test for the number of
4184 entries in the VERNEED section since the
4185 numbers for the needed versions do not start
4187 Elf_Internal_Verneed *t;
4190 for (t = elf_tdata (abfd)->verref;
4194 Elf_Internal_Vernaux *a;
4196 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4198 if (a->vna_other == vernum)
4200 verstr = a->vna_nodename;
4209 (*_bfd_error_handler)
4210 (_("%B: %s: invalid needed version %d"),
4211 abfd, name, vernum);
4212 bfd_set_error (bfd_error_bad_value);
4213 goto error_free_vers;
4217 namelen = strlen (name);
4218 verlen = strlen (verstr);
4219 newlen = namelen + verlen + 2;
4220 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4221 && isym->st_shndx != SHN_UNDEF)
4224 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4225 if (newname == NULL)
4226 goto error_free_vers;
4227 memcpy (newname, name, namelen);
4228 p = newname + namelen;
4230 /* If this is a defined non-hidden version symbol,
4231 we add another @ to the name. This indicates the
4232 default version of the symbol. */
4233 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4234 && isym->st_shndx != SHN_UNDEF)
4236 memcpy (p, verstr, verlen + 1);
4241 /* If this symbol has default visibility and the user has
4242 requested we not re-export it, then mark it as hidden. */
4243 if (!bfd_is_und_section (sec)
4246 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4247 isym->st_other = (STV_HIDDEN
4248 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4250 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4251 sym_hash, &old_bfd, &old_weak,
4252 &old_alignment, &skip, &override,
4253 &type_change_ok, &size_change_ok,
4255 goto error_free_vers;
4260 /* Override a definition only if the new symbol matches the
4262 if (override && matched)
4266 while (h->root.type == bfd_link_hash_indirect
4267 || h->root.type == bfd_link_hash_warning)
4268 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4270 if (elf_tdata (abfd)->verdef != NULL
4273 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4276 if (! (_bfd_generic_link_add_one_symbol
4277 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4278 (struct bfd_link_hash_entry **) sym_hash)))
4279 goto error_free_vers;
4282 /* We need to make sure that indirect symbol dynamic flags are
4285 while (h->root.type == bfd_link_hash_indirect
4286 || h->root.type == bfd_link_hash_warning)
4287 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4291 new_weak = (flags & BSF_WEAK) != 0;
4292 new_weakdef = FALSE;
4296 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4297 && is_elf_hash_table (htab)
4298 && h->u.weakdef == NULL)
4300 /* Keep a list of all weak defined non function symbols from
4301 a dynamic object, using the weakdef field. Later in this
4302 function we will set the weakdef field to the correct
4303 value. We only put non-function symbols from dynamic
4304 objects on this list, because that happens to be the only
4305 time we need to know the normal symbol corresponding to a
4306 weak symbol, and the information is time consuming to
4307 figure out. If the weakdef field is not already NULL,
4308 then this symbol was already defined by some previous
4309 dynamic object, and we will be using that previous
4310 definition anyhow. */
4312 h->u.weakdef = weaks;
4317 /* Set the alignment of a common symbol. */
4318 if ((common || bfd_is_com_section (sec))
4319 && h->root.type == bfd_link_hash_common)
4324 align = bfd_log2 (isym->st_value);
4327 /* The new symbol is a common symbol in a shared object.
4328 We need to get the alignment from the section. */
4329 align = new_sec->alignment_power;
4331 if (align > old_alignment)
4332 h->root.u.c.p->alignment_power = align;
4334 h->root.u.c.p->alignment_power = old_alignment;
4337 if (is_elf_hash_table (htab))
4339 /* Set a flag in the hash table entry indicating the type of
4340 reference or definition we just found. A dynamic symbol
4341 is one which is referenced or defined by both a regular
4342 object and a shared object. */
4343 bfd_boolean dynsym = FALSE;
4345 /* Plugin symbols aren't normal. Don't set def_regular or
4346 ref_regular for them, or make them dynamic. */
4347 if ((abfd->flags & BFD_PLUGIN) != 0)
4354 if (bind != STB_WEAK)
4355 h->ref_regular_nonweak = 1;
4367 /* If the indirect symbol has been forced local, don't
4368 make the real symbol dynamic. */
4369 if ((h == hi || !hi->forced_local)
4370 && (bfd_link_dll (info)
4380 hi->ref_dynamic = 1;
4385 hi->def_dynamic = 1;
4388 /* If the indirect symbol has been forced local, don't
4389 make the real symbol dynamic. */
4390 if ((h == hi || !hi->forced_local)
4393 || (h->u.weakdef != NULL
4395 && h->u.weakdef->dynindx != -1)))
4399 /* Check to see if we need to add an indirect symbol for
4400 the default name. */
4402 || (!override && h->root.type == bfd_link_hash_common))
4403 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4404 sec, value, &old_bfd, &dynsym))
4405 goto error_free_vers;
4407 /* Check the alignment when a common symbol is involved. This
4408 can change when a common symbol is overridden by a normal
4409 definition or a common symbol is ignored due to the old
4410 normal definition. We need to make sure the maximum
4411 alignment is maintained. */
4412 if ((old_alignment || common)
4413 && h->root.type != bfd_link_hash_common)
4415 unsigned int common_align;
4416 unsigned int normal_align;
4417 unsigned int symbol_align;
4421 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4422 || h->root.type == bfd_link_hash_defweak);
4424 symbol_align = ffs (h->root.u.def.value) - 1;
4425 if (h->root.u.def.section->owner != NULL
4426 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4428 normal_align = h->root.u.def.section->alignment_power;
4429 if (normal_align > symbol_align)
4430 normal_align = symbol_align;
4433 normal_align = symbol_align;
4437 common_align = old_alignment;
4438 common_bfd = old_bfd;
4443 common_align = bfd_log2 (isym->st_value);
4445 normal_bfd = old_bfd;
4448 if (normal_align < common_align)
4450 /* PR binutils/2735 */
4451 if (normal_bfd == NULL)
4452 (*_bfd_error_handler)
4453 (_("Warning: alignment %u of common symbol `%s' in %B is"
4454 " greater than the alignment (%u) of its section %A"),
4455 common_bfd, h->root.u.def.section,
4456 1 << common_align, name, 1 << normal_align);
4458 (*_bfd_error_handler)
4459 (_("Warning: alignment %u of symbol `%s' in %B"
4460 " is smaller than %u in %B"),
4461 normal_bfd, common_bfd,
4462 1 << normal_align, name, 1 << common_align);
4466 /* Remember the symbol size if it isn't undefined. */
4467 if (isym->st_size != 0
4468 && isym->st_shndx != SHN_UNDEF
4469 && (definition || h->size == 0))
4472 && h->size != isym->st_size
4473 && ! size_change_ok)
4474 (*_bfd_error_handler)
4475 (_("Warning: size of symbol `%s' changed"
4476 " from %lu in %B to %lu in %B"),
4478 name, (unsigned long) h->size,
4479 (unsigned long) isym->st_size);
4481 h->size = isym->st_size;
4484 /* If this is a common symbol, then we always want H->SIZE
4485 to be the size of the common symbol. The code just above
4486 won't fix the size if a common symbol becomes larger. We
4487 don't warn about a size change here, because that is
4488 covered by --warn-common. Allow changes between different
4490 if (h->root.type == bfd_link_hash_common)
4491 h->size = h->root.u.c.size;
4493 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4494 && ((definition && !new_weak)
4495 || (old_weak && h->root.type == bfd_link_hash_common)
4496 || h->type == STT_NOTYPE))
4498 unsigned int type = ELF_ST_TYPE (isym->st_info);
4500 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4502 if (type == STT_GNU_IFUNC
4503 && (abfd->flags & DYNAMIC) != 0)
4506 if (h->type != type)
4508 if (h->type != STT_NOTYPE && ! type_change_ok)
4509 (*_bfd_error_handler)
4510 (_("Warning: type of symbol `%s' changed"
4511 " from %d to %d in %B"),
4512 abfd, name, h->type, type);
4518 /* Merge st_other field. */
4519 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4521 /* We don't want to make debug symbol dynamic. */
4523 && (sec->flags & SEC_DEBUGGING)
4524 && !bfd_link_relocatable (info))
4527 /* Nor should we make plugin symbols dynamic. */
4528 if ((abfd->flags & BFD_PLUGIN) != 0)
4533 h->target_internal = isym->st_target_internal;
4534 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4537 if (definition && !dynamic)
4539 char *p = strchr (name, ELF_VER_CHR);
4540 if (p != NULL && p[1] != ELF_VER_CHR)
4542 /* Queue non-default versions so that .symver x, x@FOO
4543 aliases can be checked. */
4546 amt = ((isymend - isym + 1)
4547 * sizeof (struct elf_link_hash_entry *));
4549 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4551 goto error_free_vers;
4553 nondeflt_vers[nondeflt_vers_cnt++] = h;
4557 if (dynsym && h->dynindx == -1)
4559 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4560 goto error_free_vers;
4561 if (h->u.weakdef != NULL
4563 && h->u.weakdef->dynindx == -1)
4565 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4566 goto error_free_vers;
4569 else if (dynsym && h->dynindx != -1)
4570 /* If the symbol already has a dynamic index, but
4571 visibility says it should not be visible, turn it into
4573 switch (ELF_ST_VISIBILITY (h->other))
4577 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4582 /* Don't add DT_NEEDED for references from the dummy bfd nor
4583 for unmatched symbol. */
4588 && h->ref_regular_nonweak
4590 || (old_bfd->flags & BFD_PLUGIN) == 0))
4591 || (h->ref_dynamic_nonweak
4592 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4593 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4596 const char *soname = elf_dt_name (abfd);
4598 info->callbacks->minfo ("%!", soname, old_bfd,
4599 h->root.root.string);
4601 /* A symbol from a library loaded via DT_NEEDED of some
4602 other library is referenced by a regular object.
4603 Add a DT_NEEDED entry for it. Issue an error if
4604 --no-add-needed is used and the reference was not
4607 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4609 (*_bfd_error_handler)
4610 (_("%B: undefined reference to symbol '%s'"),
4612 bfd_set_error (bfd_error_missing_dso);
4613 goto error_free_vers;
4616 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4617 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4620 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4622 goto error_free_vers;
4624 BFD_ASSERT (ret == 0);
4629 if (extversym != NULL)
4635 if (isymbuf != NULL)
4641 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4645 /* Restore the symbol table. */
4646 old_ent = (char *) old_tab + tabsize;
4647 memset (elf_sym_hashes (abfd), 0,
4648 extsymcount * sizeof (struct elf_link_hash_entry *));
4649 htab->root.table.table = old_table;
4650 htab->root.table.size = old_size;
4651 htab->root.table.count = old_count;
4652 memcpy (htab->root.table.table, old_tab, tabsize);
4653 htab->root.undefs = old_undefs;
4654 htab->root.undefs_tail = old_undefs_tail;
4655 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4656 for (i = 0; i < htab->root.table.size; i++)
4658 struct bfd_hash_entry *p;
4659 struct elf_link_hash_entry *h;
4661 unsigned int alignment_power;
4663 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4665 h = (struct elf_link_hash_entry *) p;
4666 if (h->root.type == bfd_link_hash_warning)
4667 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4668 if (h->dynindx >= old_dynsymcount
4669 && h->dynstr_index < old_dynstr_size)
4670 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4672 /* Preserve the maximum alignment and size for common
4673 symbols even if this dynamic lib isn't on DT_NEEDED
4674 since it can still be loaded at run time by another
4676 if (h->root.type == bfd_link_hash_common)
4678 size = h->root.u.c.size;
4679 alignment_power = h->root.u.c.p->alignment_power;
4684 alignment_power = 0;
4686 memcpy (p, old_ent, htab->root.table.entsize);
4687 old_ent = (char *) old_ent + htab->root.table.entsize;
4688 h = (struct elf_link_hash_entry *) p;
4689 if (h->root.type == bfd_link_hash_warning)
4691 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4692 old_ent = (char *) old_ent + htab->root.table.entsize;
4693 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4695 if (h->root.type == bfd_link_hash_common)
4697 if (size > h->root.u.c.size)
4698 h->root.u.c.size = size;
4699 if (alignment_power > h->root.u.c.p->alignment_power)
4700 h->root.u.c.p->alignment_power = alignment_power;
4705 /* Make a special call to the linker "notice" function to
4706 tell it that symbols added for crefs may need to be removed. */
4707 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4708 goto error_free_vers;
4711 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4713 if (nondeflt_vers != NULL)
4714 free (nondeflt_vers);
4718 if (old_tab != NULL)
4720 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4721 goto error_free_vers;
4726 /* Now that all the symbols from this input file are created, if
4727 not performing a relocatable link, handle .symver foo, foo@BAR
4728 such that any relocs against foo become foo@BAR. */
4729 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4731 bfd_size_type cnt, symidx;
4733 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4735 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4736 char *shortname, *p;
4738 p = strchr (h->root.root.string, ELF_VER_CHR);
4740 || (h->root.type != bfd_link_hash_defined
4741 && h->root.type != bfd_link_hash_defweak))
4744 amt = p - h->root.root.string;
4745 shortname = (char *) bfd_malloc (amt + 1);
4747 goto error_free_vers;
4748 memcpy (shortname, h->root.root.string, amt);
4749 shortname[amt] = '\0';
4751 hi = (struct elf_link_hash_entry *)
4752 bfd_link_hash_lookup (&htab->root, shortname,
4753 FALSE, FALSE, FALSE);
4755 && hi->root.type == h->root.type
4756 && hi->root.u.def.value == h->root.u.def.value
4757 && hi->root.u.def.section == h->root.u.def.section)
4759 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4760 hi->root.type = bfd_link_hash_indirect;
4761 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4762 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4763 sym_hash = elf_sym_hashes (abfd);
4765 for (symidx = 0; symidx < extsymcount; ++symidx)
4766 if (sym_hash[symidx] == hi)
4768 sym_hash[symidx] = h;
4774 free (nondeflt_vers);
4775 nondeflt_vers = NULL;
4778 /* Now set the weakdefs field correctly for all the weak defined
4779 symbols we found. The only way to do this is to search all the
4780 symbols. Since we only need the information for non functions in
4781 dynamic objects, that's the only time we actually put anything on
4782 the list WEAKS. We need this information so that if a regular
4783 object refers to a symbol defined weakly in a dynamic object, the
4784 real symbol in the dynamic object is also put in the dynamic
4785 symbols; we also must arrange for both symbols to point to the
4786 same memory location. We could handle the general case of symbol
4787 aliasing, but a general symbol alias can only be generated in
4788 assembler code, handling it correctly would be very time
4789 consuming, and other ELF linkers don't handle general aliasing
4793 struct elf_link_hash_entry **hpp;
4794 struct elf_link_hash_entry **hppend;
4795 struct elf_link_hash_entry **sorted_sym_hash;
4796 struct elf_link_hash_entry *h;
4799 /* Since we have to search the whole symbol list for each weak
4800 defined symbol, search time for N weak defined symbols will be
4801 O(N^2). Binary search will cut it down to O(NlogN). */
4802 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4803 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4804 if (sorted_sym_hash == NULL)
4806 sym_hash = sorted_sym_hash;
4807 hpp = elf_sym_hashes (abfd);
4808 hppend = hpp + extsymcount;
4810 for (; hpp < hppend; hpp++)
4814 && h->root.type == bfd_link_hash_defined
4815 && !bed->is_function_type (h->type))
4823 qsort (sorted_sym_hash, sym_count,
4824 sizeof (struct elf_link_hash_entry *),
4827 while (weaks != NULL)
4829 struct elf_link_hash_entry *hlook;
4832 size_t i, j, idx = 0;
4835 weaks = hlook->u.weakdef;
4836 hlook->u.weakdef = NULL;
4838 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4839 || hlook->root.type == bfd_link_hash_defweak
4840 || hlook->root.type == bfd_link_hash_common
4841 || hlook->root.type == bfd_link_hash_indirect);
4842 slook = hlook->root.u.def.section;
4843 vlook = hlook->root.u.def.value;
4849 bfd_signed_vma vdiff;
4851 h = sorted_sym_hash[idx];
4852 vdiff = vlook - h->root.u.def.value;
4859 int sdiff = slook->id - h->root.u.def.section->id;
4869 /* We didn't find a value/section match. */
4873 /* With multiple aliases, or when the weak symbol is already
4874 strongly defined, we have multiple matching symbols and
4875 the binary search above may land on any of them. Step
4876 one past the matching symbol(s). */
4879 h = sorted_sym_hash[idx];
4880 if (h->root.u.def.section != slook
4881 || h->root.u.def.value != vlook)
4885 /* Now look back over the aliases. Since we sorted by size
4886 as well as value and section, we'll choose the one with
4887 the largest size. */
4890 h = sorted_sym_hash[idx];
4892 /* Stop if value or section doesn't match. */
4893 if (h->root.u.def.section != slook
4894 || h->root.u.def.value != vlook)
4896 else if (h != hlook)
4898 hlook->u.weakdef = h;
4900 /* If the weak definition is in the list of dynamic
4901 symbols, make sure the real definition is put
4903 if (hlook->dynindx != -1 && h->dynindx == -1)
4905 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4908 free (sorted_sym_hash);
4913 /* If the real definition is in the list of dynamic
4914 symbols, make sure the weak definition is put
4915 there as well. If we don't do this, then the
4916 dynamic loader might not merge the entries for the
4917 real definition and the weak definition. */
4918 if (h->dynindx != -1 && hlook->dynindx == -1)
4920 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4921 goto err_free_sym_hash;
4928 free (sorted_sym_hash);
4931 if (bed->check_directives
4932 && !(*bed->check_directives) (abfd, info))
4935 /* If this object is the same format as the output object, and it is
4936 not a shared library, then let the backend look through the
4939 This is required to build global offset table entries and to
4940 arrange for dynamic relocs. It is not required for the
4941 particular common case of linking non PIC code, even when linking
4942 against shared libraries, but unfortunately there is no way of
4943 knowing whether an object file has been compiled PIC or not.
4944 Looking through the relocs is not particularly time consuming.
4945 The problem is that we must either (1) keep the relocs in memory,
4946 which causes the linker to require additional runtime memory or
4947 (2) read the relocs twice from the input file, which wastes time.
4948 This would be a good case for using mmap.
4950 I have no idea how to handle linking PIC code into a file of a
4951 different format. It probably can't be done. */
4953 && is_elf_hash_table (htab)
4954 && bed->check_relocs != NULL
4955 && elf_object_id (abfd) == elf_hash_table_id (htab)
4956 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4960 for (o = abfd->sections; o != NULL; o = o->next)
4962 Elf_Internal_Rela *internal_relocs;
4965 if ((o->flags & SEC_RELOC) == 0
4966 || o->reloc_count == 0
4967 || ((info->strip == strip_all || info->strip == strip_debugger)
4968 && (o->flags & SEC_DEBUGGING) != 0)
4969 || bfd_is_abs_section (o->output_section))
4972 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4974 if (internal_relocs == NULL)
4977 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4979 if (elf_section_data (o)->relocs != internal_relocs)
4980 free (internal_relocs);
4987 /* If this is a non-traditional link, try to optimize the handling
4988 of the .stab/.stabstr sections. */
4990 && ! info->traditional_format
4991 && is_elf_hash_table (htab)
4992 && (info->strip != strip_all && info->strip != strip_debugger))
4996 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4997 if (stabstr != NULL)
4999 bfd_size_type string_offset = 0;
5002 for (stab = abfd->sections; stab; stab = stab->next)
5003 if (CONST_STRNEQ (stab->name, ".stab")
5004 && (!stab->name[5] ||
5005 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5006 && (stab->flags & SEC_MERGE) == 0
5007 && !bfd_is_abs_section (stab->output_section))
5009 struct bfd_elf_section_data *secdata;
5011 secdata = elf_section_data (stab);
5012 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5013 stabstr, &secdata->sec_info,
5016 if (secdata->sec_info)
5017 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5022 if (is_elf_hash_table (htab) && add_needed)
5024 /* Add this bfd to the loaded list. */
5025 struct elf_link_loaded_list *n;
5027 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5031 n->next = htab->loaded;
5038 if (old_tab != NULL)
5040 if (nondeflt_vers != NULL)
5041 free (nondeflt_vers);
5042 if (extversym != NULL)
5045 if (isymbuf != NULL)
5051 /* Return the linker hash table entry of a symbol that might be
5052 satisfied by an archive symbol. Return -1 on error. */
5054 struct elf_link_hash_entry *
5055 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5056 struct bfd_link_info *info,
5059 struct elf_link_hash_entry *h;
5063 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5067 /* If this is a default version (the name contains @@), look up the
5068 symbol again with only one `@' as well as without the version.
5069 The effect is that references to the symbol with and without the
5070 version will be matched by the default symbol in the archive. */
5072 p = strchr (name, ELF_VER_CHR);
5073 if (p == NULL || p[1] != ELF_VER_CHR)
5076 /* First check with only one `@'. */
5077 len = strlen (name);
5078 copy = (char *) bfd_alloc (abfd, len);
5080 return (struct elf_link_hash_entry *) 0 - 1;
5082 first = p - name + 1;
5083 memcpy (copy, name, first);
5084 memcpy (copy + first, name + first + 1, len - first);
5086 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5089 /* We also need to check references to the symbol without the
5091 copy[first - 1] = '\0';
5092 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5093 FALSE, FALSE, TRUE);
5096 bfd_release (abfd, copy);
5100 /* Add symbols from an ELF archive file to the linker hash table. We
5101 don't use _bfd_generic_link_add_archive_symbols because we need to
5102 handle versioned symbols.
5104 Fortunately, ELF archive handling is simpler than that done by
5105 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5106 oddities. In ELF, if we find a symbol in the archive map, and the
5107 symbol is currently undefined, we know that we must pull in that
5110 Unfortunately, we do have to make multiple passes over the symbol
5111 table until nothing further is resolved. */
5114 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5117 unsigned char *included = NULL;
5121 const struct elf_backend_data *bed;
5122 struct elf_link_hash_entry * (*archive_symbol_lookup)
5123 (bfd *, struct bfd_link_info *, const char *);
5125 if (! bfd_has_map (abfd))
5127 /* An empty archive is a special case. */
5128 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5130 bfd_set_error (bfd_error_no_armap);
5134 /* Keep track of all symbols we know to be already defined, and all
5135 files we know to be already included. This is to speed up the
5136 second and subsequent passes. */
5137 c = bfd_ardata (abfd)->symdef_count;
5141 amt *= sizeof (*included);
5142 included = (unsigned char *) bfd_zmalloc (amt);
5143 if (included == NULL)
5146 symdefs = bfd_ardata (abfd)->symdefs;
5147 bed = get_elf_backend_data (abfd);
5148 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5161 symdefend = symdef + c;
5162 for (i = 0; symdef < symdefend; symdef++, i++)
5164 struct elf_link_hash_entry *h;
5166 struct bfd_link_hash_entry *undefs_tail;
5171 if (symdef->file_offset == last)
5177 h = archive_symbol_lookup (abfd, info, symdef->name);
5178 if (h == (struct elf_link_hash_entry *) 0 - 1)
5184 if (h->root.type == bfd_link_hash_common)
5186 /* We currently have a common symbol. The archive map contains
5187 a reference to this symbol, so we may want to include it. We
5188 only want to include it however, if this archive element
5189 contains a definition of the symbol, not just another common
5192 Unfortunately some archivers (including GNU ar) will put
5193 declarations of common symbols into their archive maps, as
5194 well as real definitions, so we cannot just go by the archive
5195 map alone. Instead we must read in the element's symbol
5196 table and check that to see what kind of symbol definition
5198 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5201 else if (h->root.type != bfd_link_hash_undefined)
5203 if (h->root.type != bfd_link_hash_undefweak)
5204 /* Symbol must be defined. Don't check it again. */
5209 /* We need to include this archive member. */
5210 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5211 if (element == NULL)
5214 if (! bfd_check_format (element, bfd_object))
5217 undefs_tail = info->hash->undefs_tail;
5219 if (!(*info->callbacks
5220 ->add_archive_element) (info, element, symdef->name, &element))
5222 if (!bfd_link_add_symbols (element, info))
5225 /* If there are any new undefined symbols, we need to make
5226 another pass through the archive in order to see whether
5227 they can be defined. FIXME: This isn't perfect, because
5228 common symbols wind up on undefs_tail and because an
5229 undefined symbol which is defined later on in this pass
5230 does not require another pass. This isn't a bug, but it
5231 does make the code less efficient than it could be. */
5232 if (undefs_tail != info->hash->undefs_tail)
5235 /* Look backward to mark all symbols from this object file
5236 which we have already seen in this pass. */
5240 included[mark] = TRUE;
5245 while (symdefs[mark].file_offset == symdef->file_offset);
5247 /* We mark subsequent symbols from this object file as we go
5248 on through the loop. */
5249 last = symdef->file_offset;
5259 if (included != NULL)
5264 /* Given an ELF BFD, add symbols to the global hash table as
5268 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5270 switch (bfd_get_format (abfd))
5273 return elf_link_add_object_symbols (abfd, info);
5275 return elf_link_add_archive_symbols (abfd, info);
5277 bfd_set_error (bfd_error_wrong_format);
5282 struct hash_codes_info
5284 unsigned long *hashcodes;
5288 /* This function will be called though elf_link_hash_traverse to store
5289 all hash value of the exported symbols in an array. */
5292 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5294 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5299 /* Ignore indirect symbols. These are added by the versioning code. */
5300 if (h->dynindx == -1)
5303 name = h->root.root.string;
5304 if (h->versioned >= versioned)
5306 char *p = strchr (name, ELF_VER_CHR);
5309 alc = (char *) bfd_malloc (p - name + 1);
5315 memcpy (alc, name, p - name);
5316 alc[p - name] = '\0';
5321 /* Compute the hash value. */
5322 ha = bfd_elf_hash (name);
5324 /* Store the found hash value in the array given as the argument. */
5325 *(inf->hashcodes)++ = ha;
5327 /* And store it in the struct so that we can put it in the hash table
5329 h->u.elf_hash_value = ha;
5337 struct collect_gnu_hash_codes
5340 const struct elf_backend_data *bed;
5341 unsigned long int nsyms;
5342 unsigned long int maskbits;
5343 unsigned long int *hashcodes;
5344 unsigned long int *hashval;
5345 unsigned long int *indx;
5346 unsigned long int *counts;
5349 long int min_dynindx;
5350 unsigned long int bucketcount;
5351 unsigned long int symindx;
5352 long int local_indx;
5353 long int shift1, shift2;
5354 unsigned long int mask;
5358 /* This function will be called though elf_link_hash_traverse to store
5359 all hash value of the exported symbols in an array. */
5362 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5364 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5369 /* Ignore indirect symbols. These are added by the versioning code. */
5370 if (h->dynindx == -1)
5373 /* Ignore also local symbols and undefined symbols. */
5374 if (! (*s->bed->elf_hash_symbol) (h))
5377 name = h->root.root.string;
5378 if (h->versioned >= versioned)
5380 char *p = strchr (name, ELF_VER_CHR);
5383 alc = (char *) bfd_malloc (p - name + 1);
5389 memcpy (alc, name, p - name);
5390 alc[p - name] = '\0';
5395 /* Compute the hash value. */
5396 ha = bfd_elf_gnu_hash (name);
5398 /* Store the found hash value in the array for compute_bucket_count,
5399 and also for .dynsym reordering purposes. */
5400 s->hashcodes[s->nsyms] = ha;
5401 s->hashval[h->dynindx] = ha;
5403 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5404 s->min_dynindx = h->dynindx;
5412 /* This function will be called though elf_link_hash_traverse to do
5413 final dynaminc symbol renumbering. */
5416 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5418 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5419 unsigned long int bucket;
5420 unsigned long int val;
5422 /* Ignore indirect symbols. */
5423 if (h->dynindx == -1)
5426 /* Ignore also local symbols and undefined symbols. */
5427 if (! (*s->bed->elf_hash_symbol) (h))
5429 if (h->dynindx >= s->min_dynindx)
5430 h->dynindx = s->local_indx++;
5434 bucket = s->hashval[h->dynindx] % s->bucketcount;
5435 val = (s->hashval[h->dynindx] >> s->shift1)
5436 & ((s->maskbits >> s->shift1) - 1);
5437 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5439 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5440 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5441 if (s->counts[bucket] == 1)
5442 /* Last element terminates the chain. */
5444 bfd_put_32 (s->output_bfd, val,
5445 s->contents + (s->indx[bucket] - s->symindx) * 4);
5446 --s->counts[bucket];
5447 h->dynindx = s->indx[bucket]++;
5451 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5454 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5456 return !(h->forced_local
5457 || h->root.type == bfd_link_hash_undefined
5458 || h->root.type == bfd_link_hash_undefweak
5459 || ((h->root.type == bfd_link_hash_defined
5460 || h->root.type == bfd_link_hash_defweak)
5461 && h->root.u.def.section->output_section == NULL));
5464 /* Array used to determine the number of hash table buckets to use
5465 based on the number of symbols there are. If there are fewer than
5466 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5467 fewer than 37 we use 17 buckets, and so forth. We never use more
5468 than 32771 buckets. */
5470 static const size_t elf_buckets[] =
5472 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5476 /* Compute bucket count for hashing table. We do not use a static set
5477 of possible tables sizes anymore. Instead we determine for all
5478 possible reasonable sizes of the table the outcome (i.e., the
5479 number of collisions etc) and choose the best solution. The
5480 weighting functions are not too simple to allow the table to grow
5481 without bounds. Instead one of the weighting factors is the size.
5482 Therefore the result is always a good payoff between few collisions
5483 (= short chain lengths) and table size. */
5485 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5486 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5487 unsigned long int nsyms,
5490 size_t best_size = 0;
5491 unsigned long int i;
5493 /* We have a problem here. The following code to optimize the table
5494 size requires an integer type with more the 32 bits. If
5495 BFD_HOST_U_64_BIT is set we know about such a type. */
5496 #ifdef BFD_HOST_U_64_BIT
5501 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5502 bfd *dynobj = elf_hash_table (info)->dynobj;
5503 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5504 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5505 unsigned long int *counts;
5507 unsigned int no_improvement_count = 0;
5509 /* Possible optimization parameters: if we have NSYMS symbols we say
5510 that the hashing table must at least have NSYMS/4 and at most
5512 minsize = nsyms / 4;
5515 best_size = maxsize = nsyms * 2;
5520 if ((best_size & 31) == 0)
5524 /* Create array where we count the collisions in. We must use bfd_malloc
5525 since the size could be large. */
5527 amt *= sizeof (unsigned long int);
5528 counts = (unsigned long int *) bfd_malloc (amt);
5532 /* Compute the "optimal" size for the hash table. The criteria is a
5533 minimal chain length. The minor criteria is (of course) the size
5535 for (i = minsize; i < maxsize; ++i)
5537 /* Walk through the array of hashcodes and count the collisions. */
5538 BFD_HOST_U_64_BIT max;
5539 unsigned long int j;
5540 unsigned long int fact;
5542 if (gnu_hash && (i & 31) == 0)
5545 memset (counts, '\0', i * sizeof (unsigned long int));
5547 /* Determine how often each hash bucket is used. */
5548 for (j = 0; j < nsyms; ++j)
5549 ++counts[hashcodes[j] % i];
5551 /* For the weight function we need some information about the
5552 pagesize on the target. This is information need not be 100%
5553 accurate. Since this information is not available (so far) we
5554 define it here to a reasonable default value. If it is crucial
5555 to have a better value some day simply define this value. */
5556 # ifndef BFD_TARGET_PAGESIZE
5557 # define BFD_TARGET_PAGESIZE (4096)
5560 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5562 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5565 /* Variant 1: optimize for short chains. We add the squares
5566 of all the chain lengths (which favors many small chain
5567 over a few long chains). */
5568 for (j = 0; j < i; ++j)
5569 max += counts[j] * counts[j];
5571 /* This adds penalties for the overall size of the table. */
5572 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5575 /* Variant 2: Optimize a lot more for small table. Here we
5576 also add squares of the size but we also add penalties for
5577 empty slots (the +1 term). */
5578 for (j = 0; j < i; ++j)
5579 max += (1 + counts[j]) * (1 + counts[j]);
5581 /* The overall size of the table is considered, but not as
5582 strong as in variant 1, where it is squared. */
5583 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5587 /* Compare with current best results. */
5588 if (max < best_chlen)
5592 no_improvement_count = 0;
5594 /* PR 11843: Avoid futile long searches for the best bucket size
5595 when there are a large number of symbols. */
5596 else if (++no_improvement_count == 100)
5603 #endif /* defined (BFD_HOST_U_64_BIT) */
5605 /* This is the fallback solution if no 64bit type is available or if we
5606 are not supposed to spend much time on optimizations. We select the
5607 bucket count using a fixed set of numbers. */
5608 for (i = 0; elf_buckets[i] != 0; i++)
5610 best_size = elf_buckets[i];
5611 if (nsyms < elf_buckets[i + 1])
5614 if (gnu_hash && best_size < 2)
5621 /* Size any SHT_GROUP section for ld -r. */
5624 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5628 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5629 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5630 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5635 /* Set a default stack segment size. The value in INFO wins. If it
5636 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5637 undefined it is initialized. */
5640 bfd_elf_stack_segment_size (bfd *output_bfd,
5641 struct bfd_link_info *info,
5642 const char *legacy_symbol,
5643 bfd_vma default_size)
5645 struct elf_link_hash_entry *h = NULL;
5647 /* Look for legacy symbol. */
5649 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5650 FALSE, FALSE, FALSE);
5651 if (h && (h->root.type == bfd_link_hash_defined
5652 || h->root.type == bfd_link_hash_defweak)
5654 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5656 /* The symbol has no type if specified on the command line. */
5657 h->type = STT_OBJECT;
5658 if (info->stacksize)
5659 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5660 output_bfd, legacy_symbol);
5661 else if (h->root.u.def.section != bfd_abs_section_ptr)
5662 (*_bfd_error_handler) (_("%B: %s not absolute"),
5663 output_bfd, legacy_symbol);
5665 info->stacksize = h->root.u.def.value;
5668 if (!info->stacksize)
5669 /* If the user didn't set a size, or explicitly inhibit the
5670 size, set it now. */
5671 info->stacksize = default_size;
5673 /* Provide the legacy symbol, if it is referenced. */
5674 if (h && (h->root.type == bfd_link_hash_undefined
5675 || h->root.type == bfd_link_hash_undefweak))
5677 struct bfd_link_hash_entry *bh = NULL;
5679 if (!(_bfd_generic_link_add_one_symbol
5680 (info, output_bfd, legacy_symbol,
5681 BSF_GLOBAL, bfd_abs_section_ptr,
5682 info->stacksize >= 0 ? info->stacksize : 0,
5683 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5686 h = (struct elf_link_hash_entry *) bh;
5688 h->type = STT_OBJECT;
5694 /* Set up the sizes and contents of the ELF dynamic sections. This is
5695 called by the ELF linker emulation before_allocation routine. We
5696 must set the sizes of the sections before the linker sets the
5697 addresses of the various sections. */
5700 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5703 const char *filter_shlib,
5705 const char *depaudit,
5706 const char * const *auxiliary_filters,
5707 struct bfd_link_info *info,
5708 asection **sinterpptr)
5710 bfd_size_type soname_indx;
5712 const struct elf_backend_data *bed;
5713 struct elf_info_failed asvinfo;
5717 soname_indx = (bfd_size_type) -1;
5719 if (!is_elf_hash_table (info->hash))
5722 bed = get_elf_backend_data (output_bfd);
5724 /* Any syms created from now on start with -1 in
5725 got.refcount/offset and plt.refcount/offset. */
5726 elf_hash_table (info)->init_got_refcount
5727 = elf_hash_table (info)->init_got_offset;
5728 elf_hash_table (info)->init_plt_refcount
5729 = elf_hash_table (info)->init_plt_offset;
5731 if (bfd_link_relocatable (info)
5732 && !_bfd_elf_size_group_sections (info))
5735 /* The backend may have to create some sections regardless of whether
5736 we're dynamic or not. */
5737 if (bed->elf_backend_always_size_sections
5738 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5741 /* Determine any GNU_STACK segment requirements, after the backend
5742 has had a chance to set a default segment size. */
5743 if (info->execstack)
5744 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5745 else if (info->noexecstack)
5746 elf_stack_flags (output_bfd) = PF_R | PF_W;
5750 asection *notesec = NULL;
5753 for (inputobj = info->input_bfds;
5755 inputobj = inputobj->link.next)
5760 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5762 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5765 if (s->flags & SEC_CODE)
5769 else if (bed->default_execstack)
5772 if (notesec || info->stacksize > 0)
5773 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5774 if (notesec && exec && bfd_link_relocatable (info)
5775 && notesec->output_section != bfd_abs_section_ptr)
5776 notesec->output_section->flags |= SEC_CODE;
5779 dynobj = elf_hash_table (info)->dynobj;
5781 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5783 struct elf_info_failed eif;
5784 struct elf_link_hash_entry *h;
5786 struct bfd_elf_version_tree *t;
5787 struct bfd_elf_version_expr *d;
5789 bfd_boolean all_defined;
5791 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5792 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5796 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5798 if (soname_indx == (bfd_size_type) -1
5799 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5805 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5807 info->flags |= DF_SYMBOLIC;
5815 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5817 if (indx == (bfd_size_type) -1)
5820 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5821 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5825 if (filter_shlib != NULL)
5829 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5830 filter_shlib, TRUE);
5831 if (indx == (bfd_size_type) -1
5832 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5836 if (auxiliary_filters != NULL)
5838 const char * const *p;
5840 for (p = auxiliary_filters; *p != NULL; p++)
5844 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5846 if (indx == (bfd_size_type) -1
5847 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5856 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5858 if (indx == (bfd_size_type) -1
5859 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5863 if (depaudit != NULL)
5867 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5869 if (indx == (bfd_size_type) -1
5870 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5877 /* If we are supposed to export all symbols into the dynamic symbol
5878 table (this is not the normal case), then do so. */
5879 if (info->export_dynamic
5880 || (bfd_link_executable (info) && info->dynamic))
5882 elf_link_hash_traverse (elf_hash_table (info),
5883 _bfd_elf_export_symbol,
5889 /* Make all global versions with definition. */
5890 for (t = info->version_info; t != NULL; t = t->next)
5891 for (d = t->globals.list; d != NULL; d = d->next)
5892 if (!d->symver && d->literal)
5894 const char *verstr, *name;
5895 size_t namelen, verlen, newlen;
5896 char *newname, *p, leading_char;
5897 struct elf_link_hash_entry *newh;
5899 leading_char = bfd_get_symbol_leading_char (output_bfd);
5901 namelen = strlen (name) + (leading_char != '\0');
5903 verlen = strlen (verstr);
5904 newlen = namelen + verlen + 3;
5906 newname = (char *) bfd_malloc (newlen);
5907 if (newname == NULL)
5909 newname[0] = leading_char;
5910 memcpy (newname + (leading_char != '\0'), name, namelen);
5912 /* Check the hidden versioned definition. */
5913 p = newname + namelen;
5915 memcpy (p, verstr, verlen + 1);
5916 newh = elf_link_hash_lookup (elf_hash_table (info),
5917 newname, FALSE, FALSE,
5920 || (newh->root.type != bfd_link_hash_defined
5921 && newh->root.type != bfd_link_hash_defweak))
5923 /* Check the default versioned definition. */
5925 memcpy (p, verstr, verlen + 1);
5926 newh = elf_link_hash_lookup (elf_hash_table (info),
5927 newname, FALSE, FALSE,
5932 /* Mark this version if there is a definition and it is
5933 not defined in a shared object. */
5935 && !newh->def_dynamic
5936 && (newh->root.type == bfd_link_hash_defined
5937 || newh->root.type == bfd_link_hash_defweak))
5941 /* Attach all the symbols to their version information. */
5942 asvinfo.info = info;
5943 asvinfo.failed = FALSE;
5945 elf_link_hash_traverse (elf_hash_table (info),
5946 _bfd_elf_link_assign_sym_version,
5951 if (!info->allow_undefined_version)
5953 /* Check if all global versions have a definition. */
5955 for (t = info->version_info; t != NULL; t = t->next)
5956 for (d = t->globals.list; d != NULL; d = d->next)
5957 if (d->literal && !d->symver && !d->script)
5959 (*_bfd_error_handler)
5960 (_("%s: undefined version: %s"),
5961 d->pattern, t->name);
5962 all_defined = FALSE;
5967 bfd_set_error (bfd_error_bad_value);
5972 /* Find all symbols which were defined in a dynamic object and make
5973 the backend pick a reasonable value for them. */
5974 elf_link_hash_traverse (elf_hash_table (info),
5975 _bfd_elf_adjust_dynamic_symbol,
5980 /* Add some entries to the .dynamic section. We fill in some of the
5981 values later, in bfd_elf_final_link, but we must add the entries
5982 now so that we know the final size of the .dynamic section. */
5984 /* If there are initialization and/or finalization functions to
5985 call then add the corresponding DT_INIT/DT_FINI entries. */
5986 h = (info->init_function
5987 ? elf_link_hash_lookup (elf_hash_table (info),
5988 info->init_function, FALSE,
5995 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5998 h = (info->fini_function
5999 ? elf_link_hash_lookup (elf_hash_table (info),
6000 info->fini_function, FALSE,
6007 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6011 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6012 if (s != NULL && s->linker_has_input)
6014 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6015 if (! bfd_link_executable (info))
6020 for (sub = info->input_bfds; sub != NULL;
6021 sub = sub->link.next)
6022 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6023 for (o = sub->sections; o != NULL; o = o->next)
6024 if (elf_section_data (o)->this_hdr.sh_type
6025 == SHT_PREINIT_ARRAY)
6027 (*_bfd_error_handler)
6028 (_("%B: .preinit_array section is not allowed in DSO"),
6033 bfd_set_error (bfd_error_nonrepresentable_section);
6037 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6038 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6041 s = bfd_get_section_by_name (output_bfd, ".init_array");
6042 if (s != NULL && s->linker_has_input)
6044 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6045 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6048 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6049 if (s != NULL && s->linker_has_input)
6051 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6052 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6056 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6057 /* If .dynstr is excluded from the link, we don't want any of
6058 these tags. Strictly, we should be checking each section
6059 individually; This quick check covers for the case where
6060 someone does a /DISCARD/ : { *(*) }. */
6061 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6063 bfd_size_type strsize;
6065 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6066 if ((info->emit_hash
6067 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6068 || (info->emit_gnu_hash
6069 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6070 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6071 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6072 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6073 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6074 bed->s->sizeof_sym))
6079 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6082 /* The backend must work out the sizes of all the other dynamic
6085 && bed->elf_backend_size_dynamic_sections != NULL
6086 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6089 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6091 unsigned long section_sym_count;
6092 struct bfd_elf_version_tree *verdefs;
6095 /* Set up the version definition section. */
6096 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6097 BFD_ASSERT (s != NULL);
6099 /* We may have created additional version definitions if we are
6100 just linking a regular application. */
6101 verdefs = info->version_info;
6103 /* Skip anonymous version tag. */
6104 if (verdefs != NULL && verdefs->vernum == 0)
6105 verdefs = verdefs->next;
6107 if (verdefs == NULL && !info->create_default_symver)
6108 s->flags |= SEC_EXCLUDE;
6113 struct bfd_elf_version_tree *t;
6115 Elf_Internal_Verdef def;
6116 Elf_Internal_Verdaux defaux;
6117 struct bfd_link_hash_entry *bh;
6118 struct elf_link_hash_entry *h;
6124 /* Make space for the base version. */
6125 size += sizeof (Elf_External_Verdef);
6126 size += sizeof (Elf_External_Verdaux);
6129 /* Make space for the default version. */
6130 if (info->create_default_symver)
6132 size += sizeof (Elf_External_Verdef);
6136 for (t = verdefs; t != NULL; t = t->next)
6138 struct bfd_elf_version_deps *n;
6140 /* Don't emit base version twice. */
6144 size += sizeof (Elf_External_Verdef);
6145 size += sizeof (Elf_External_Verdaux);
6148 for (n = t->deps; n != NULL; n = n->next)
6149 size += sizeof (Elf_External_Verdaux);
6153 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6154 if (s->contents == NULL && s->size != 0)
6157 /* Fill in the version definition section. */
6161 def.vd_version = VER_DEF_CURRENT;
6162 def.vd_flags = VER_FLG_BASE;
6165 if (info->create_default_symver)
6167 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6168 def.vd_next = sizeof (Elf_External_Verdef);
6172 def.vd_aux = sizeof (Elf_External_Verdef);
6173 def.vd_next = (sizeof (Elf_External_Verdef)
6174 + sizeof (Elf_External_Verdaux));
6177 if (soname_indx != (bfd_size_type) -1)
6179 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6181 def.vd_hash = bfd_elf_hash (soname);
6182 defaux.vda_name = soname_indx;
6189 name = lbasename (output_bfd->filename);
6190 def.vd_hash = bfd_elf_hash (name);
6191 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6193 if (indx == (bfd_size_type) -1)
6195 defaux.vda_name = indx;
6197 defaux.vda_next = 0;
6199 _bfd_elf_swap_verdef_out (output_bfd, &def,
6200 (Elf_External_Verdef *) p);
6201 p += sizeof (Elf_External_Verdef);
6202 if (info->create_default_symver)
6204 /* Add a symbol representing this version. */
6206 if (! (_bfd_generic_link_add_one_symbol
6207 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6209 get_elf_backend_data (dynobj)->collect, &bh)))
6211 h = (struct elf_link_hash_entry *) bh;
6214 h->type = STT_OBJECT;
6215 h->verinfo.vertree = NULL;
6217 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6220 /* Create a duplicate of the base version with the same
6221 aux block, but different flags. */
6224 def.vd_aux = sizeof (Elf_External_Verdef);
6226 def.vd_next = (sizeof (Elf_External_Verdef)
6227 + sizeof (Elf_External_Verdaux));
6230 _bfd_elf_swap_verdef_out (output_bfd, &def,
6231 (Elf_External_Verdef *) p);
6232 p += sizeof (Elf_External_Verdef);
6234 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6235 (Elf_External_Verdaux *) p);
6236 p += sizeof (Elf_External_Verdaux);
6238 for (t = verdefs; t != NULL; t = t->next)
6241 struct bfd_elf_version_deps *n;
6243 /* Don't emit the base version twice. */
6248 for (n = t->deps; n != NULL; n = n->next)
6251 /* Add a symbol representing this version. */
6253 if (! (_bfd_generic_link_add_one_symbol
6254 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6256 get_elf_backend_data (dynobj)->collect, &bh)))
6258 h = (struct elf_link_hash_entry *) bh;
6261 h->type = STT_OBJECT;
6262 h->verinfo.vertree = t;
6264 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6267 def.vd_version = VER_DEF_CURRENT;
6269 if (t->globals.list == NULL
6270 && t->locals.list == NULL
6272 def.vd_flags |= VER_FLG_WEAK;
6273 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6274 def.vd_cnt = cdeps + 1;
6275 def.vd_hash = bfd_elf_hash (t->name);
6276 def.vd_aux = sizeof (Elf_External_Verdef);
6279 /* If a basever node is next, it *must* be the last node in
6280 the chain, otherwise Verdef construction breaks. */
6281 if (t->next != NULL && t->next->vernum == 0)
6282 BFD_ASSERT (t->next->next == NULL);
6284 if (t->next != NULL && t->next->vernum != 0)
6285 def.vd_next = (sizeof (Elf_External_Verdef)
6286 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6288 _bfd_elf_swap_verdef_out (output_bfd, &def,
6289 (Elf_External_Verdef *) p);
6290 p += sizeof (Elf_External_Verdef);
6292 defaux.vda_name = h->dynstr_index;
6293 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6295 defaux.vda_next = 0;
6296 if (t->deps != NULL)
6297 defaux.vda_next = sizeof (Elf_External_Verdaux);
6298 t->name_indx = defaux.vda_name;
6300 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6301 (Elf_External_Verdaux *) p);
6302 p += sizeof (Elf_External_Verdaux);
6304 for (n = t->deps; n != NULL; n = n->next)
6306 if (n->version_needed == NULL)
6308 /* This can happen if there was an error in the
6310 defaux.vda_name = 0;
6314 defaux.vda_name = n->version_needed->name_indx;
6315 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6318 if (n->next == NULL)
6319 defaux.vda_next = 0;
6321 defaux.vda_next = sizeof (Elf_External_Verdaux);
6323 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6324 (Elf_External_Verdaux *) p);
6325 p += sizeof (Elf_External_Verdaux);
6329 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6330 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6333 elf_tdata (output_bfd)->cverdefs = cdefs;
6336 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6338 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6341 else if (info->flags & DF_BIND_NOW)
6343 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6349 if (bfd_link_executable (info))
6350 info->flags_1 &= ~ (DF_1_INITFIRST
6353 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6357 /* Work out the size of the version reference section. */
6359 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6360 BFD_ASSERT (s != NULL);
6362 struct elf_find_verdep_info sinfo;
6365 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6366 if (sinfo.vers == 0)
6368 sinfo.failed = FALSE;
6370 elf_link_hash_traverse (elf_hash_table (info),
6371 _bfd_elf_link_find_version_dependencies,
6376 if (elf_tdata (output_bfd)->verref == NULL)
6377 s->flags |= SEC_EXCLUDE;
6380 Elf_Internal_Verneed *t;
6385 /* Build the version dependency section. */
6388 for (t = elf_tdata (output_bfd)->verref;
6392 Elf_Internal_Vernaux *a;
6394 size += sizeof (Elf_External_Verneed);
6396 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6397 size += sizeof (Elf_External_Vernaux);
6401 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6402 if (s->contents == NULL)
6406 for (t = elf_tdata (output_bfd)->verref;
6411 Elf_Internal_Vernaux *a;
6415 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6418 t->vn_version = VER_NEED_CURRENT;
6420 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6421 elf_dt_name (t->vn_bfd) != NULL
6422 ? elf_dt_name (t->vn_bfd)
6423 : lbasename (t->vn_bfd->filename),
6425 if (indx == (bfd_size_type) -1)
6428 t->vn_aux = sizeof (Elf_External_Verneed);
6429 if (t->vn_nextref == NULL)
6432 t->vn_next = (sizeof (Elf_External_Verneed)
6433 + caux * sizeof (Elf_External_Vernaux));
6435 _bfd_elf_swap_verneed_out (output_bfd, t,
6436 (Elf_External_Verneed *) p);
6437 p += sizeof (Elf_External_Verneed);
6439 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6441 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6442 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6443 a->vna_nodename, FALSE);
6444 if (indx == (bfd_size_type) -1)
6447 if (a->vna_nextptr == NULL)
6450 a->vna_next = sizeof (Elf_External_Vernaux);
6452 _bfd_elf_swap_vernaux_out (output_bfd, a,
6453 (Elf_External_Vernaux *) p);
6454 p += sizeof (Elf_External_Vernaux);
6458 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6459 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6462 elf_tdata (output_bfd)->cverrefs = crefs;
6466 if ((elf_tdata (output_bfd)->cverrefs == 0
6467 && elf_tdata (output_bfd)->cverdefs == 0)
6468 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6469 §ion_sym_count) == 0)
6471 s = bfd_get_linker_section (dynobj, ".gnu.version");
6472 s->flags |= SEC_EXCLUDE;
6478 /* Find the first non-excluded output section. We'll use its
6479 section symbol for some emitted relocs. */
6481 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6485 for (s = output_bfd->sections; s != NULL; s = s->next)
6486 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6487 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6489 elf_hash_table (info)->text_index_section = s;
6494 /* Find two non-excluded output sections, one for code, one for data.
6495 We'll use their section symbols for some emitted relocs. */
6497 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6501 /* Data first, since setting text_index_section changes
6502 _bfd_elf_link_omit_section_dynsym. */
6503 for (s = output_bfd->sections; s != NULL; s = s->next)
6504 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6505 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6507 elf_hash_table (info)->data_index_section = s;
6511 for (s = output_bfd->sections; s != NULL; s = s->next)
6512 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6513 == (SEC_ALLOC | SEC_READONLY))
6514 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6516 elf_hash_table (info)->text_index_section = s;
6520 if (elf_hash_table (info)->text_index_section == NULL)
6521 elf_hash_table (info)->text_index_section
6522 = elf_hash_table (info)->data_index_section;
6526 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6528 const struct elf_backend_data *bed;
6530 if (!is_elf_hash_table (info->hash))
6533 bed = get_elf_backend_data (output_bfd);
6534 (*bed->elf_backend_init_index_section) (output_bfd, info);
6536 if (elf_hash_table (info)->dynamic_sections_created)
6540 bfd_size_type dynsymcount;
6541 unsigned long section_sym_count;
6542 unsigned int dtagcount;
6544 dynobj = elf_hash_table (info)->dynobj;
6546 /* Assign dynsym indicies. In a shared library we generate a
6547 section symbol for each output section, which come first.
6548 Next come all of the back-end allocated local dynamic syms,
6549 followed by the rest of the global symbols. */
6551 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6552 §ion_sym_count);
6554 /* Work out the size of the symbol version section. */
6555 s = bfd_get_linker_section (dynobj, ".gnu.version");
6556 BFD_ASSERT (s != NULL);
6557 if (dynsymcount != 0
6558 && (s->flags & SEC_EXCLUDE) == 0)
6560 s->size = dynsymcount * sizeof (Elf_External_Versym);
6561 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6562 if (s->contents == NULL)
6565 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6569 /* Set the size of the .dynsym and .hash sections. We counted
6570 the number of dynamic symbols in elf_link_add_object_symbols.
6571 We will build the contents of .dynsym and .hash when we build
6572 the final symbol table, because until then we do not know the
6573 correct value to give the symbols. We built the .dynstr
6574 section as we went along in elf_link_add_object_symbols. */
6575 s = elf_hash_table (info)->dynsym;
6576 BFD_ASSERT (s != NULL);
6577 s->size = dynsymcount * bed->s->sizeof_sym;
6579 if (dynsymcount != 0)
6581 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6582 if (s->contents == NULL)
6585 /* The first entry in .dynsym is a dummy symbol.
6586 Clear all the section syms, in case we don't output them all. */
6587 ++section_sym_count;
6588 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6591 elf_hash_table (info)->bucketcount = 0;
6593 /* Compute the size of the hashing table. As a side effect this
6594 computes the hash values for all the names we export. */
6595 if (info->emit_hash)
6597 unsigned long int *hashcodes;
6598 struct hash_codes_info hashinf;
6600 unsigned long int nsyms;
6602 size_t hash_entry_size;
6604 /* Compute the hash values for all exported symbols. At the same
6605 time store the values in an array so that we could use them for
6607 amt = dynsymcount * sizeof (unsigned long int);
6608 hashcodes = (unsigned long int *) bfd_malloc (amt);
6609 if (hashcodes == NULL)
6611 hashinf.hashcodes = hashcodes;
6612 hashinf.error = FALSE;
6614 /* Put all hash values in HASHCODES. */
6615 elf_link_hash_traverse (elf_hash_table (info),
6616 elf_collect_hash_codes, &hashinf);
6623 nsyms = hashinf.hashcodes - hashcodes;
6625 = compute_bucket_count (info, hashcodes, nsyms, 0);
6628 if (bucketcount == 0)
6631 elf_hash_table (info)->bucketcount = bucketcount;
6633 s = bfd_get_linker_section (dynobj, ".hash");
6634 BFD_ASSERT (s != NULL);
6635 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6636 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6637 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6638 if (s->contents == NULL)
6641 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6642 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6643 s->contents + hash_entry_size);
6646 if (info->emit_gnu_hash)
6649 unsigned char *contents;
6650 struct collect_gnu_hash_codes cinfo;
6654 memset (&cinfo, 0, sizeof (cinfo));
6656 /* Compute the hash values for all exported symbols. At the same
6657 time store the values in an array so that we could use them for
6659 amt = dynsymcount * 2 * sizeof (unsigned long int);
6660 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6661 if (cinfo.hashcodes == NULL)
6664 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6665 cinfo.min_dynindx = -1;
6666 cinfo.output_bfd = output_bfd;
6669 /* Put all hash values in HASHCODES. */
6670 elf_link_hash_traverse (elf_hash_table (info),
6671 elf_collect_gnu_hash_codes, &cinfo);
6674 free (cinfo.hashcodes);
6679 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6681 if (bucketcount == 0)
6683 free (cinfo.hashcodes);
6687 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6688 BFD_ASSERT (s != NULL);
6690 if (cinfo.nsyms == 0)
6692 /* Empty .gnu.hash section is special. */
6693 BFD_ASSERT (cinfo.min_dynindx == -1);
6694 free (cinfo.hashcodes);
6695 s->size = 5 * 4 + bed->s->arch_size / 8;
6696 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6697 if (contents == NULL)
6699 s->contents = contents;
6700 /* 1 empty bucket. */
6701 bfd_put_32 (output_bfd, 1, contents);
6702 /* SYMIDX above the special symbol 0. */
6703 bfd_put_32 (output_bfd, 1, contents + 4);
6704 /* Just one word for bitmask. */
6705 bfd_put_32 (output_bfd, 1, contents + 8);
6706 /* Only hash fn bloom filter. */
6707 bfd_put_32 (output_bfd, 0, contents + 12);
6708 /* No hashes are valid - empty bitmask. */
6709 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6710 /* No hashes in the only bucket. */
6711 bfd_put_32 (output_bfd, 0,
6712 contents + 16 + bed->s->arch_size / 8);
6716 unsigned long int maskwords, maskbitslog2, x;
6717 BFD_ASSERT (cinfo.min_dynindx != -1);
6721 while ((x >>= 1) != 0)
6723 if (maskbitslog2 < 3)
6725 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6726 maskbitslog2 = maskbitslog2 + 3;
6728 maskbitslog2 = maskbitslog2 + 2;
6729 if (bed->s->arch_size == 64)
6731 if (maskbitslog2 == 5)
6737 cinfo.mask = (1 << cinfo.shift1) - 1;
6738 cinfo.shift2 = maskbitslog2;
6739 cinfo.maskbits = 1 << maskbitslog2;
6740 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6741 amt = bucketcount * sizeof (unsigned long int) * 2;
6742 amt += maskwords * sizeof (bfd_vma);
6743 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6744 if (cinfo.bitmask == NULL)
6746 free (cinfo.hashcodes);
6750 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6751 cinfo.indx = cinfo.counts + bucketcount;
6752 cinfo.symindx = dynsymcount - cinfo.nsyms;
6753 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6755 /* Determine how often each hash bucket is used. */
6756 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6757 for (i = 0; i < cinfo.nsyms; ++i)
6758 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6760 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6761 if (cinfo.counts[i] != 0)
6763 cinfo.indx[i] = cnt;
6764 cnt += cinfo.counts[i];
6766 BFD_ASSERT (cnt == dynsymcount);
6767 cinfo.bucketcount = bucketcount;
6768 cinfo.local_indx = cinfo.min_dynindx;
6770 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6771 s->size += cinfo.maskbits / 8;
6772 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6773 if (contents == NULL)
6775 free (cinfo.bitmask);
6776 free (cinfo.hashcodes);
6780 s->contents = contents;
6781 bfd_put_32 (output_bfd, bucketcount, contents);
6782 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6783 bfd_put_32 (output_bfd, maskwords, contents + 8);
6784 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6785 contents += 16 + cinfo.maskbits / 8;
6787 for (i = 0; i < bucketcount; ++i)
6789 if (cinfo.counts[i] == 0)
6790 bfd_put_32 (output_bfd, 0, contents);
6792 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6796 cinfo.contents = contents;
6798 /* Renumber dynamic symbols, populate .gnu.hash section. */
6799 elf_link_hash_traverse (elf_hash_table (info),
6800 elf_renumber_gnu_hash_syms, &cinfo);
6802 contents = s->contents + 16;
6803 for (i = 0; i < maskwords; ++i)
6805 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6807 contents += bed->s->arch_size / 8;
6810 free (cinfo.bitmask);
6811 free (cinfo.hashcodes);
6815 s = bfd_get_linker_section (dynobj, ".dynstr");
6816 BFD_ASSERT (s != NULL);
6818 elf_finalize_dynstr (output_bfd, info);
6820 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6822 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6823 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6830 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6833 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6836 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6837 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6840 /* Finish SHF_MERGE section merging. */
6843 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6848 if (!is_elf_hash_table (info->hash))
6851 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6852 if ((ibfd->flags & DYNAMIC) == 0
6853 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6854 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6855 == get_elf_backend_data (obfd)->s->elfclass))
6856 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6857 if ((sec->flags & SEC_MERGE) != 0
6858 && !bfd_is_abs_section (sec->output_section))
6860 struct bfd_elf_section_data *secdata;
6862 secdata = elf_section_data (sec);
6863 if (! _bfd_add_merge_section (obfd,
6864 &elf_hash_table (info)->merge_info,
6865 sec, &secdata->sec_info))
6867 else if (secdata->sec_info)
6868 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6871 if (elf_hash_table (info)->merge_info != NULL)
6872 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6873 merge_sections_remove_hook);
6877 /* Create an entry in an ELF linker hash table. */
6879 struct bfd_hash_entry *
6880 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6881 struct bfd_hash_table *table,
6884 /* Allocate the structure if it has not already been allocated by a
6888 entry = (struct bfd_hash_entry *)
6889 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6894 /* Call the allocation method of the superclass. */
6895 entry = _bfd_link_hash_newfunc (entry, table, string);
6898 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6899 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6901 /* Set local fields. */
6904 ret->got = htab->init_got_refcount;
6905 ret->plt = htab->init_plt_refcount;
6906 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6907 - offsetof (struct elf_link_hash_entry, size)));
6908 /* Assume that we have been called by a non-ELF symbol reader.
6909 This flag is then reset by the code which reads an ELF input
6910 file. This ensures that a symbol created by a non-ELF symbol
6911 reader will have the flag set correctly. */
6918 /* Copy data from an indirect symbol to its direct symbol, hiding the
6919 old indirect symbol. Also used for copying flags to a weakdef. */
6922 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6923 struct elf_link_hash_entry *dir,
6924 struct elf_link_hash_entry *ind)
6926 struct elf_link_hash_table *htab;
6928 /* Copy down any references that we may have already seen to the
6929 symbol which just became indirect if DIR isn't a hidden versioned
6932 if (dir->versioned != versioned_hidden)
6934 dir->ref_dynamic |= ind->ref_dynamic;
6935 dir->ref_regular |= ind->ref_regular;
6936 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6937 dir->non_got_ref |= ind->non_got_ref;
6938 dir->needs_plt |= ind->needs_plt;
6939 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6942 if (ind->root.type != bfd_link_hash_indirect)
6945 /* Copy over the global and procedure linkage table refcount entries.
6946 These may have been already set up by a check_relocs routine. */
6947 htab = elf_hash_table (info);
6948 if (ind->got.refcount > htab->init_got_refcount.refcount)
6950 if (dir->got.refcount < 0)
6951 dir->got.refcount = 0;
6952 dir->got.refcount += ind->got.refcount;
6953 ind->got.refcount = htab->init_got_refcount.refcount;
6956 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6958 if (dir->plt.refcount < 0)
6959 dir->plt.refcount = 0;
6960 dir->plt.refcount += ind->plt.refcount;
6961 ind->plt.refcount = htab->init_plt_refcount.refcount;
6964 if (ind->dynindx != -1)
6966 if (dir->dynindx != -1)
6967 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6968 dir->dynindx = ind->dynindx;
6969 dir->dynstr_index = ind->dynstr_index;
6971 ind->dynstr_index = 0;
6976 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6977 struct elf_link_hash_entry *h,
6978 bfd_boolean force_local)
6980 /* STT_GNU_IFUNC symbol must go through PLT. */
6981 if (h->type != STT_GNU_IFUNC)
6983 h->plt = elf_hash_table (info)->init_plt_offset;
6988 h->forced_local = 1;
6989 if (h->dynindx != -1)
6992 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6998 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7002 _bfd_elf_link_hash_table_init
7003 (struct elf_link_hash_table *table,
7005 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7006 struct bfd_hash_table *,
7008 unsigned int entsize,
7009 enum elf_target_id target_id)
7012 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7014 table->init_got_refcount.refcount = can_refcount - 1;
7015 table->init_plt_refcount.refcount = can_refcount - 1;
7016 table->init_got_offset.offset = -(bfd_vma) 1;
7017 table->init_plt_offset.offset = -(bfd_vma) 1;
7018 /* The first dynamic symbol is a dummy. */
7019 table->dynsymcount = 1;
7021 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7023 table->root.type = bfd_link_elf_hash_table;
7024 table->hash_table_id = target_id;
7029 /* Create an ELF linker hash table. */
7031 struct bfd_link_hash_table *
7032 _bfd_elf_link_hash_table_create (bfd *abfd)
7034 struct elf_link_hash_table *ret;
7035 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7037 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7041 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7042 sizeof (struct elf_link_hash_entry),
7048 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7053 /* Destroy an ELF linker hash table. */
7056 _bfd_elf_link_hash_table_free (bfd *obfd)
7058 struct elf_link_hash_table *htab;
7060 htab = (struct elf_link_hash_table *) obfd->link.hash;
7061 if (htab->dynstr != NULL)
7062 _bfd_elf_strtab_free (htab->dynstr);
7063 _bfd_merge_sections_free (htab->merge_info);
7064 _bfd_generic_link_hash_table_free (obfd);
7067 /* This is a hook for the ELF emulation code in the generic linker to
7068 tell the backend linker what file name to use for the DT_NEEDED
7069 entry for a dynamic object. */
7072 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7074 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7075 && bfd_get_format (abfd) == bfd_object)
7076 elf_dt_name (abfd) = name;
7080 bfd_elf_get_dyn_lib_class (bfd *abfd)
7083 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7084 && bfd_get_format (abfd) == bfd_object)
7085 lib_class = elf_dyn_lib_class (abfd);
7092 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7094 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7095 && bfd_get_format (abfd) == bfd_object)
7096 elf_dyn_lib_class (abfd) = lib_class;
7099 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7100 the linker ELF emulation code. */
7102 struct bfd_link_needed_list *
7103 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7104 struct bfd_link_info *info)
7106 if (! is_elf_hash_table (info->hash))
7108 return elf_hash_table (info)->needed;
7111 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7112 hook for the linker ELF emulation code. */
7114 struct bfd_link_needed_list *
7115 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7116 struct bfd_link_info *info)
7118 if (! is_elf_hash_table (info->hash))
7120 return elf_hash_table (info)->runpath;
7123 /* Get the name actually used for a dynamic object for a link. This
7124 is the SONAME entry if there is one. Otherwise, it is the string
7125 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7128 bfd_elf_get_dt_soname (bfd *abfd)
7130 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7131 && bfd_get_format (abfd) == bfd_object)
7132 return elf_dt_name (abfd);
7136 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7137 the ELF linker emulation code. */
7140 bfd_elf_get_bfd_needed_list (bfd *abfd,
7141 struct bfd_link_needed_list **pneeded)
7144 bfd_byte *dynbuf = NULL;
7145 unsigned int elfsec;
7146 unsigned long shlink;
7147 bfd_byte *extdyn, *extdynend;
7149 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7153 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7154 || bfd_get_format (abfd) != bfd_object)
7157 s = bfd_get_section_by_name (abfd, ".dynamic");
7158 if (s == NULL || s->size == 0)
7161 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7164 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7165 if (elfsec == SHN_BAD)
7168 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7170 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7171 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7174 extdynend = extdyn + s->size;
7175 for (; extdyn < extdynend; extdyn += extdynsize)
7177 Elf_Internal_Dyn dyn;
7179 (*swap_dyn_in) (abfd, extdyn, &dyn);
7181 if (dyn.d_tag == DT_NULL)
7184 if (dyn.d_tag == DT_NEEDED)
7187 struct bfd_link_needed_list *l;
7188 unsigned int tagv = dyn.d_un.d_val;
7191 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7196 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7217 struct elf_symbuf_symbol
7219 unsigned long st_name; /* Symbol name, index in string tbl */
7220 unsigned char st_info; /* Type and binding attributes */
7221 unsigned char st_other; /* Visibilty, and target specific */
7224 struct elf_symbuf_head
7226 struct elf_symbuf_symbol *ssym;
7227 bfd_size_type count;
7228 unsigned int st_shndx;
7235 Elf_Internal_Sym *isym;
7236 struct elf_symbuf_symbol *ssym;
7241 /* Sort references to symbols by ascending section number. */
7244 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7246 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7247 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7249 return s1->st_shndx - s2->st_shndx;
7253 elf_sym_name_compare (const void *arg1, const void *arg2)
7255 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7256 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7257 return strcmp (s1->name, s2->name);
7260 static struct elf_symbuf_head *
7261 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7263 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7264 struct elf_symbuf_symbol *ssym;
7265 struct elf_symbuf_head *ssymbuf, *ssymhead;
7266 bfd_size_type i, shndx_count, total_size;
7268 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7272 for (ind = indbuf, i = 0; i < symcount; i++)
7273 if (isymbuf[i].st_shndx != SHN_UNDEF)
7274 *ind++ = &isymbuf[i];
7277 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7278 elf_sort_elf_symbol);
7281 if (indbufend > indbuf)
7282 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7283 if (ind[0]->st_shndx != ind[1]->st_shndx)
7286 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7287 + (indbufend - indbuf) * sizeof (*ssym));
7288 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7289 if (ssymbuf == NULL)
7295 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7296 ssymbuf->ssym = NULL;
7297 ssymbuf->count = shndx_count;
7298 ssymbuf->st_shndx = 0;
7299 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7301 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7304 ssymhead->ssym = ssym;
7305 ssymhead->count = 0;
7306 ssymhead->st_shndx = (*ind)->st_shndx;
7308 ssym->st_name = (*ind)->st_name;
7309 ssym->st_info = (*ind)->st_info;
7310 ssym->st_other = (*ind)->st_other;
7313 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7314 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7321 /* Check if 2 sections define the same set of local and global
7325 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7326 struct bfd_link_info *info)
7329 const struct elf_backend_data *bed1, *bed2;
7330 Elf_Internal_Shdr *hdr1, *hdr2;
7331 bfd_size_type symcount1, symcount2;
7332 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7333 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7334 Elf_Internal_Sym *isym, *isymend;
7335 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7336 bfd_size_type count1, count2, i;
7337 unsigned int shndx1, shndx2;
7343 /* Both sections have to be in ELF. */
7344 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7345 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7348 if (elf_section_type (sec1) != elf_section_type (sec2))
7351 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7352 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7353 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7356 bed1 = get_elf_backend_data (bfd1);
7357 bed2 = get_elf_backend_data (bfd2);
7358 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7359 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7360 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7361 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7363 if (symcount1 == 0 || symcount2 == 0)
7369 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7370 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7372 if (ssymbuf1 == NULL)
7374 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7376 if (isymbuf1 == NULL)
7379 if (!info->reduce_memory_overheads)
7380 elf_tdata (bfd1)->symbuf = ssymbuf1
7381 = elf_create_symbuf (symcount1, isymbuf1);
7384 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7386 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7388 if (isymbuf2 == NULL)
7391 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7392 elf_tdata (bfd2)->symbuf = ssymbuf2
7393 = elf_create_symbuf (symcount2, isymbuf2);
7396 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7398 /* Optimized faster version. */
7399 bfd_size_type lo, hi, mid;
7400 struct elf_symbol *symp;
7401 struct elf_symbuf_symbol *ssym, *ssymend;
7404 hi = ssymbuf1->count;
7409 mid = (lo + hi) / 2;
7410 if (shndx1 < ssymbuf1[mid].st_shndx)
7412 else if (shndx1 > ssymbuf1[mid].st_shndx)
7416 count1 = ssymbuf1[mid].count;
7423 hi = ssymbuf2->count;
7428 mid = (lo + hi) / 2;
7429 if (shndx2 < ssymbuf2[mid].st_shndx)
7431 else if (shndx2 > ssymbuf2[mid].st_shndx)
7435 count2 = ssymbuf2[mid].count;
7441 if (count1 == 0 || count2 == 0 || count1 != count2)
7445 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7447 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7448 if (symtable1 == NULL || symtable2 == NULL)
7452 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7453 ssym < ssymend; ssym++, symp++)
7455 symp->u.ssym = ssym;
7456 symp->name = bfd_elf_string_from_elf_section (bfd1,
7462 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7463 ssym < ssymend; ssym++, symp++)
7465 symp->u.ssym = ssym;
7466 symp->name = bfd_elf_string_from_elf_section (bfd2,
7471 /* Sort symbol by name. */
7472 qsort (symtable1, count1, sizeof (struct elf_symbol),
7473 elf_sym_name_compare);
7474 qsort (symtable2, count1, sizeof (struct elf_symbol),
7475 elf_sym_name_compare);
7477 for (i = 0; i < count1; i++)
7478 /* Two symbols must have the same binding, type and name. */
7479 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7480 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7481 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7488 symtable1 = (struct elf_symbol *)
7489 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7490 symtable2 = (struct elf_symbol *)
7491 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7492 if (symtable1 == NULL || symtable2 == NULL)
7495 /* Count definitions in the section. */
7497 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7498 if (isym->st_shndx == shndx1)
7499 symtable1[count1++].u.isym = isym;
7502 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7503 if (isym->st_shndx == shndx2)
7504 symtable2[count2++].u.isym = isym;
7506 if (count1 == 0 || count2 == 0 || count1 != count2)
7509 for (i = 0; i < count1; i++)
7511 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7512 symtable1[i].u.isym->st_name);
7514 for (i = 0; i < count2; i++)
7516 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7517 symtable2[i].u.isym->st_name);
7519 /* Sort symbol by name. */
7520 qsort (symtable1, count1, sizeof (struct elf_symbol),
7521 elf_sym_name_compare);
7522 qsort (symtable2, count1, sizeof (struct elf_symbol),
7523 elf_sym_name_compare);
7525 for (i = 0; i < count1; i++)
7526 /* Two symbols must have the same binding, type and name. */
7527 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7528 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7529 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7547 /* Return TRUE if 2 section types are compatible. */
7550 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7551 bfd *bbfd, const asection *bsec)
7555 || abfd->xvec->flavour != bfd_target_elf_flavour
7556 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7559 return elf_section_type (asec) == elf_section_type (bsec);
7562 /* Final phase of ELF linker. */
7564 /* A structure we use to avoid passing large numbers of arguments. */
7566 struct elf_final_link_info
7568 /* General link information. */
7569 struct bfd_link_info *info;
7572 /* Symbol string table. */
7573 struct elf_strtab_hash *symstrtab;
7574 /* .hash section. */
7576 /* symbol version section (.gnu.version). */
7577 asection *symver_sec;
7578 /* Buffer large enough to hold contents of any section. */
7580 /* Buffer large enough to hold external relocs of any section. */
7581 void *external_relocs;
7582 /* Buffer large enough to hold internal relocs of any section. */
7583 Elf_Internal_Rela *internal_relocs;
7584 /* Buffer large enough to hold external local symbols of any input
7586 bfd_byte *external_syms;
7587 /* And a buffer for symbol section indices. */
7588 Elf_External_Sym_Shndx *locsym_shndx;
7589 /* Buffer large enough to hold internal local symbols of any input
7591 Elf_Internal_Sym *internal_syms;
7592 /* Array large enough to hold a symbol index for each local symbol
7593 of any input BFD. */
7595 /* Array large enough to hold a section pointer for each local
7596 symbol of any input BFD. */
7597 asection **sections;
7598 /* Buffer for SHT_SYMTAB_SHNDX section. */
7599 Elf_External_Sym_Shndx *symshndxbuf;
7600 /* Number of STT_FILE syms seen. */
7601 size_t filesym_count;
7604 /* This struct is used to pass information to elf_link_output_extsym. */
7606 struct elf_outext_info
7609 bfd_boolean localsyms;
7610 bfd_boolean file_sym_done;
7611 struct elf_final_link_info *flinfo;
7615 /* Support for evaluating a complex relocation.
7617 Complex relocations are generalized, self-describing relocations. The
7618 implementation of them consists of two parts: complex symbols, and the
7619 relocations themselves.
7621 The relocations are use a reserved elf-wide relocation type code (R_RELC
7622 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7623 information (start bit, end bit, word width, etc) into the addend. This
7624 information is extracted from CGEN-generated operand tables within gas.
7626 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7627 internal) representing prefix-notation expressions, including but not
7628 limited to those sorts of expressions normally encoded as addends in the
7629 addend field. The symbol mangling format is:
7632 | <unary-operator> ':' <node>
7633 | <binary-operator> ':' <node> ':' <node>
7636 <literal> := 's' <digits=N> ':' <N character symbol name>
7637 | 'S' <digits=N> ':' <N character section name>
7641 <binary-operator> := as in C
7642 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7645 set_symbol_value (bfd *bfd_with_globals,
7646 Elf_Internal_Sym *isymbuf,
7651 struct elf_link_hash_entry **sym_hashes;
7652 struct elf_link_hash_entry *h;
7653 size_t extsymoff = locsymcount;
7655 if (symidx < locsymcount)
7657 Elf_Internal_Sym *sym;
7659 sym = isymbuf + symidx;
7660 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7662 /* It is a local symbol: move it to the
7663 "absolute" section and give it a value. */
7664 sym->st_shndx = SHN_ABS;
7665 sym->st_value = val;
7668 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7672 /* It is a global symbol: set its link type
7673 to "defined" and give it a value. */
7675 sym_hashes = elf_sym_hashes (bfd_with_globals);
7676 h = sym_hashes [symidx - extsymoff];
7677 while (h->root.type == bfd_link_hash_indirect
7678 || h->root.type == bfd_link_hash_warning)
7679 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7680 h->root.type = bfd_link_hash_defined;
7681 h->root.u.def.value = val;
7682 h->root.u.def.section = bfd_abs_section_ptr;
7686 resolve_symbol (const char *name,
7688 struct elf_final_link_info *flinfo,
7690 Elf_Internal_Sym *isymbuf,
7693 Elf_Internal_Sym *sym;
7694 struct bfd_link_hash_entry *global_entry;
7695 const char *candidate = NULL;
7696 Elf_Internal_Shdr *symtab_hdr;
7699 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7701 for (i = 0; i < locsymcount; ++ i)
7705 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7708 candidate = bfd_elf_string_from_elf_section (input_bfd,
7709 symtab_hdr->sh_link,
7712 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7713 name, candidate, (unsigned long) sym->st_value);
7715 if (candidate && strcmp (candidate, name) == 0)
7717 asection *sec = flinfo->sections [i];
7719 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7720 *result += sec->output_offset + sec->output_section->vma;
7722 printf ("Found symbol with value %8.8lx\n",
7723 (unsigned long) *result);
7729 /* Hmm, haven't found it yet. perhaps it is a global. */
7730 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7731 FALSE, FALSE, TRUE);
7735 if (global_entry->type == bfd_link_hash_defined
7736 || global_entry->type == bfd_link_hash_defweak)
7738 *result = (global_entry->u.def.value
7739 + global_entry->u.def.section->output_section->vma
7740 + global_entry->u.def.section->output_offset);
7742 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7743 global_entry->root.string, (unsigned long) *result);
7752 resolve_section (const char *name,
7759 for (curr = sections; curr; curr = curr->next)
7760 if (strcmp (curr->name, name) == 0)
7762 *result = curr->vma;
7766 /* Hmm. still haven't found it. try pseudo-section names. */
7767 for (curr = sections; curr; curr = curr->next)
7769 len = strlen (curr->name);
7770 if (len > strlen (name))
7773 if (strncmp (curr->name, name, len) == 0)
7775 if (strncmp (".end", name + len, 4) == 0)
7777 *result = curr->vma + curr->size;
7781 /* Insert more pseudo-section names here, if you like. */
7789 undefined_reference (const char *reftype, const char *name)
7791 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7796 eval_symbol (bfd_vma *result,
7799 struct elf_final_link_info *flinfo,
7801 Elf_Internal_Sym *isymbuf,
7810 const char *sym = *symp;
7812 bfd_boolean symbol_is_section = FALSE;
7817 if (len < 1 || len > sizeof (symbuf))
7819 bfd_set_error (bfd_error_invalid_operation);
7832 *result = strtoul (sym, (char **) symp, 16);
7836 symbol_is_section = TRUE;
7839 symlen = strtol (sym, (char **) symp, 10);
7840 sym = *symp + 1; /* Skip the trailing ':'. */
7842 if (symend < sym || symlen + 1 > sizeof (symbuf))
7844 bfd_set_error (bfd_error_invalid_operation);
7848 memcpy (symbuf, sym, symlen);
7849 symbuf[symlen] = '\0';
7850 *symp = sym + symlen;
7852 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7853 the symbol as a section, or vice-versa. so we're pretty liberal in our
7854 interpretation here; section means "try section first", not "must be a
7855 section", and likewise with symbol. */
7857 if (symbol_is_section)
7859 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7860 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7861 isymbuf, locsymcount))
7863 undefined_reference ("section", symbuf);
7869 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7870 isymbuf, locsymcount)
7871 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7874 undefined_reference ("symbol", symbuf);
7881 /* All that remains are operators. */
7883 #define UNARY_OP(op) \
7884 if (strncmp (sym, #op, strlen (#op)) == 0) \
7886 sym += strlen (#op); \
7890 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7891 isymbuf, locsymcount, signed_p)) \
7894 *result = op ((bfd_signed_vma) a); \
7900 #define BINARY_OP(op) \
7901 if (strncmp (sym, #op, strlen (#op)) == 0) \
7903 sym += strlen (#op); \
7907 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7908 isymbuf, locsymcount, signed_p)) \
7911 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7912 isymbuf, locsymcount, signed_p)) \
7915 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7945 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7946 bfd_set_error (bfd_error_invalid_operation);
7952 put_value (bfd_vma size,
7953 unsigned long chunksz,
7958 location += (size - chunksz);
7960 for (; size; size -= chunksz, location -= chunksz)
7965 bfd_put_8 (input_bfd, x, location);
7969 bfd_put_16 (input_bfd, x, location);
7973 bfd_put_32 (input_bfd, x, location);
7974 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7980 bfd_put_64 (input_bfd, x, location);
7981 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7994 get_value (bfd_vma size,
7995 unsigned long chunksz,
8002 /* Sanity checks. */
8003 BFD_ASSERT (chunksz <= sizeof (x)
8006 && (size % chunksz) == 0
8007 && input_bfd != NULL
8008 && location != NULL);
8010 if (chunksz == sizeof (x))
8012 BFD_ASSERT (size == chunksz);
8014 /* Make sure that we do not perform an undefined shift operation.
8015 We know that size == chunksz so there will only be one iteration
8016 of the loop below. */
8020 shift = 8 * chunksz;
8022 for (; size; size -= chunksz, location += chunksz)
8027 x = (x << shift) | bfd_get_8 (input_bfd, location);
8030 x = (x << shift) | bfd_get_16 (input_bfd, location);
8033 x = (x << shift) | bfd_get_32 (input_bfd, location);
8037 x = (x << shift) | bfd_get_64 (input_bfd, location);
8048 decode_complex_addend (unsigned long *start, /* in bits */
8049 unsigned long *oplen, /* in bits */
8050 unsigned long *len, /* in bits */
8051 unsigned long *wordsz, /* in bytes */
8052 unsigned long *chunksz, /* in bytes */
8053 unsigned long *lsb0_p,
8054 unsigned long *signed_p,
8055 unsigned long *trunc_p,
8056 unsigned long encoded)
8058 * start = encoded & 0x3F;
8059 * len = (encoded >> 6) & 0x3F;
8060 * oplen = (encoded >> 12) & 0x3F;
8061 * wordsz = (encoded >> 18) & 0xF;
8062 * chunksz = (encoded >> 22) & 0xF;
8063 * lsb0_p = (encoded >> 27) & 1;
8064 * signed_p = (encoded >> 28) & 1;
8065 * trunc_p = (encoded >> 29) & 1;
8068 bfd_reloc_status_type
8069 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8070 asection *input_section ATTRIBUTE_UNUSED,
8072 Elf_Internal_Rela *rel,
8075 bfd_vma shift, x, mask;
8076 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8077 bfd_reloc_status_type r;
8079 /* Perform this reloc, since it is complex.
8080 (this is not to say that it necessarily refers to a complex
8081 symbol; merely that it is a self-describing CGEN based reloc.
8082 i.e. the addend has the complete reloc information (bit start, end,
8083 word size, etc) encoded within it.). */
8085 decode_complex_addend (&start, &oplen, &len, &wordsz,
8086 &chunksz, &lsb0_p, &signed_p,
8087 &trunc_p, rel->r_addend);
8089 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8092 shift = (start + 1) - len;
8094 shift = (8 * wordsz) - (start + len);
8096 /* FIXME: octets_per_byte. */
8097 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
8100 printf ("Doing complex reloc: "
8101 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8102 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8103 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8104 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8105 oplen, (unsigned long) x, (unsigned long) mask,
8106 (unsigned long) relocation);
8111 /* Now do an overflow check. */
8112 r = bfd_check_overflow ((signed_p
8113 ? complain_overflow_signed
8114 : complain_overflow_unsigned),
8115 len, 0, (8 * wordsz),
8119 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8122 printf (" relocation: %8.8lx\n"
8123 " shifted mask: %8.8lx\n"
8124 " shifted/masked reloc: %8.8lx\n"
8125 " result: %8.8lx\n",
8126 (unsigned long) relocation, (unsigned long) (mask << shift),
8127 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8129 /* FIXME: octets_per_byte. */
8130 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
8134 /* Functions to read r_offset from external (target order) reloc
8135 entry. Faster than bfd_getl32 et al, because we let the compiler
8136 know the value is aligned. */
8139 ext32l_r_offset (const void *p)
8146 const union aligned32 *a
8147 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8149 uint32_t aval = ( (uint32_t) a->c[0]
8150 | (uint32_t) a->c[1] << 8
8151 | (uint32_t) a->c[2] << 16
8152 | (uint32_t) a->c[3] << 24);
8157 ext32b_r_offset (const void *p)
8164 const union aligned32 *a
8165 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8167 uint32_t aval = ( (uint32_t) a->c[0] << 24
8168 | (uint32_t) a->c[1] << 16
8169 | (uint32_t) a->c[2] << 8
8170 | (uint32_t) a->c[3]);
8174 #ifdef BFD_HOST_64_BIT
8176 ext64l_r_offset (const void *p)
8183 const union aligned64 *a
8184 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8186 uint64_t aval = ( (uint64_t) a->c[0]
8187 | (uint64_t) a->c[1] << 8
8188 | (uint64_t) a->c[2] << 16
8189 | (uint64_t) a->c[3] << 24
8190 | (uint64_t) a->c[4] << 32
8191 | (uint64_t) a->c[5] << 40
8192 | (uint64_t) a->c[6] << 48
8193 | (uint64_t) a->c[7] << 56);
8198 ext64b_r_offset (const void *p)
8205 const union aligned64 *a
8206 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8208 uint64_t aval = ( (uint64_t) a->c[0] << 56
8209 | (uint64_t) a->c[1] << 48
8210 | (uint64_t) a->c[2] << 40
8211 | (uint64_t) a->c[3] << 32
8212 | (uint64_t) a->c[4] << 24
8213 | (uint64_t) a->c[5] << 16
8214 | (uint64_t) a->c[6] << 8
8215 | (uint64_t) a->c[7]);
8220 /* When performing a relocatable link, the input relocations are
8221 preserved. But, if they reference global symbols, the indices
8222 referenced must be updated. Update all the relocations found in
8226 elf_link_adjust_relocs (bfd *abfd,
8227 struct bfd_elf_section_reloc_data *reldata,
8231 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8233 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8234 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8235 bfd_vma r_type_mask;
8237 unsigned int count = reldata->count;
8238 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8240 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8242 swap_in = bed->s->swap_reloc_in;
8243 swap_out = bed->s->swap_reloc_out;
8245 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8247 swap_in = bed->s->swap_reloca_in;
8248 swap_out = bed->s->swap_reloca_out;
8253 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8256 if (bed->s->arch_size == 32)
8263 r_type_mask = 0xffffffff;
8267 erela = reldata->hdr->contents;
8268 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8270 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8273 if (*rel_hash == NULL)
8276 BFD_ASSERT ((*rel_hash)->indx >= 0);
8278 (*swap_in) (abfd, erela, irela);
8279 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8280 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8281 | (irela[j].r_info & r_type_mask));
8282 (*swap_out) (abfd, irela, erela);
8285 if (sort && count != 0)
8287 bfd_vma (*ext_r_off) (const void *);
8290 bfd_byte *base, *end, *p, *loc;
8291 bfd_byte *buf = NULL;
8293 if (bed->s->arch_size == 32)
8295 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8296 ext_r_off = ext32l_r_offset;
8297 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8298 ext_r_off = ext32b_r_offset;
8304 #ifdef BFD_HOST_64_BIT
8305 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8306 ext_r_off = ext64l_r_offset;
8307 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8308 ext_r_off = ext64b_r_offset;
8314 /* Must use a stable sort here. A modified insertion sort,
8315 since the relocs are mostly sorted already. */
8316 elt_size = reldata->hdr->sh_entsize;
8317 base = reldata->hdr->contents;
8318 end = base + count * elt_size;
8319 if (elt_size > sizeof (Elf64_External_Rela))
8322 /* Ensure the first element is lowest. This acts as a sentinel,
8323 speeding the main loop below. */
8324 r_off = (*ext_r_off) (base);
8325 for (p = loc = base; (p += elt_size) < end; )
8327 bfd_vma r_off2 = (*ext_r_off) (p);
8336 /* Don't just swap *base and *loc as that changes the order
8337 of the original base[0] and base[1] if they happen to
8338 have the same r_offset. */
8339 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8340 memcpy (onebuf, loc, elt_size);
8341 memmove (base + elt_size, base, loc - base);
8342 memcpy (base, onebuf, elt_size);
8345 for (p = base + elt_size; (p += elt_size) < end; )
8347 /* base to p is sorted, *p is next to insert. */
8348 r_off = (*ext_r_off) (p);
8349 /* Search the sorted region for location to insert. */
8351 while (r_off < (*ext_r_off) (loc))
8356 /* Chances are there is a run of relocs to insert here,
8357 from one of more input files. Files are not always
8358 linked in order due to the way elf_link_input_bfd is
8359 called. See pr17666. */
8360 size_t sortlen = p - loc;
8361 bfd_vma r_off2 = (*ext_r_off) (loc);
8362 size_t runlen = elt_size;
8363 size_t buf_size = 96 * 1024;
8364 while (p + runlen < end
8365 && (sortlen <= buf_size
8366 || runlen + elt_size <= buf_size)
8367 && r_off2 > (*ext_r_off) (p + runlen))
8371 buf = bfd_malloc (buf_size);
8375 if (runlen < sortlen)
8377 memcpy (buf, p, runlen);
8378 memmove (loc + runlen, loc, sortlen);
8379 memcpy (loc, buf, runlen);
8383 memcpy (buf, loc, sortlen);
8384 memmove (loc, p, runlen);
8385 memcpy (loc + runlen, buf, sortlen);
8387 p += runlen - elt_size;
8390 /* Hashes are no longer valid. */
8391 free (reldata->hashes);
8392 reldata->hashes = NULL;
8398 struct elf_link_sort_rela
8404 enum elf_reloc_type_class type;
8405 /* We use this as an array of size int_rels_per_ext_rel. */
8406 Elf_Internal_Rela rela[1];
8410 elf_link_sort_cmp1 (const void *A, const void *B)
8412 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8413 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8414 int relativea, relativeb;
8416 relativea = a->type == reloc_class_relative;
8417 relativeb = b->type == reloc_class_relative;
8419 if (relativea < relativeb)
8421 if (relativea > relativeb)
8423 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8425 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8427 if (a->rela->r_offset < b->rela->r_offset)
8429 if (a->rela->r_offset > b->rela->r_offset)
8435 elf_link_sort_cmp2 (const void *A, const void *B)
8437 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8438 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8440 if (a->type < b->type)
8442 if (a->type > b->type)
8444 if (a->u.offset < b->u.offset)
8446 if (a->u.offset > b->u.offset)
8448 if (a->rela->r_offset < b->rela->r_offset)
8450 if (a->rela->r_offset > b->rela->r_offset)
8456 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8458 asection *dynamic_relocs;
8461 bfd_size_type count, size;
8462 size_t i, ret, sort_elt, ext_size;
8463 bfd_byte *sort, *s_non_relative, *p;
8464 struct elf_link_sort_rela *sq;
8465 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8466 int i2e = bed->s->int_rels_per_ext_rel;
8467 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8468 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8469 struct bfd_link_order *lo;
8471 bfd_boolean use_rela;
8473 /* Find a dynamic reloc section. */
8474 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8475 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8476 if (rela_dyn != NULL && rela_dyn->size > 0
8477 && rel_dyn != NULL && rel_dyn->size > 0)
8479 bfd_boolean use_rela_initialised = FALSE;
8481 /* This is just here to stop gcc from complaining.
8482 It's initialization checking code is not perfect. */
8485 /* Both sections are present. Examine the sizes
8486 of the indirect sections to help us choose. */
8487 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8488 if (lo->type == bfd_indirect_link_order)
8490 asection *o = lo->u.indirect.section;
8492 if ((o->size % bed->s->sizeof_rela) == 0)
8494 if ((o->size % bed->s->sizeof_rel) == 0)
8495 /* Section size is divisible by both rel and rela sizes.
8496 It is of no help to us. */
8500 /* Section size is only divisible by rela. */
8501 if (use_rela_initialised && (use_rela == FALSE))
8504 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8505 bfd_set_error (bfd_error_invalid_operation);
8511 use_rela_initialised = TRUE;
8515 else if ((o->size % bed->s->sizeof_rel) == 0)
8517 /* Section size is only divisible by rel. */
8518 if (use_rela_initialised && (use_rela == TRUE))
8521 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8522 bfd_set_error (bfd_error_invalid_operation);
8528 use_rela_initialised = TRUE;
8533 /* The section size is not divisible by either - something is wrong. */
8535 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8536 bfd_set_error (bfd_error_invalid_operation);
8541 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8542 if (lo->type == bfd_indirect_link_order)
8544 asection *o = lo->u.indirect.section;
8546 if ((o->size % bed->s->sizeof_rela) == 0)
8548 if ((o->size % bed->s->sizeof_rel) == 0)
8549 /* Section size is divisible by both rel and rela sizes.
8550 It is of no help to us. */
8554 /* Section size is only divisible by rela. */
8555 if (use_rela_initialised && (use_rela == FALSE))
8558 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8559 bfd_set_error (bfd_error_invalid_operation);
8565 use_rela_initialised = TRUE;
8569 else if ((o->size % bed->s->sizeof_rel) == 0)
8571 /* Section size is only divisible by rel. */
8572 if (use_rela_initialised && (use_rela == TRUE))
8575 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8576 bfd_set_error (bfd_error_invalid_operation);
8582 use_rela_initialised = TRUE;
8587 /* The section size is not divisible by either - something is wrong. */
8589 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8590 bfd_set_error (bfd_error_invalid_operation);
8595 if (! use_rela_initialised)
8599 else if (rela_dyn != NULL && rela_dyn->size > 0)
8601 else if (rel_dyn != NULL && rel_dyn->size > 0)
8608 dynamic_relocs = rela_dyn;
8609 ext_size = bed->s->sizeof_rela;
8610 swap_in = bed->s->swap_reloca_in;
8611 swap_out = bed->s->swap_reloca_out;
8615 dynamic_relocs = rel_dyn;
8616 ext_size = bed->s->sizeof_rel;
8617 swap_in = bed->s->swap_reloc_in;
8618 swap_out = bed->s->swap_reloc_out;
8622 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8623 if (lo->type == bfd_indirect_link_order)
8624 size += lo->u.indirect.section->size;
8626 if (size != dynamic_relocs->size)
8629 sort_elt = (sizeof (struct elf_link_sort_rela)
8630 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8632 count = dynamic_relocs->size / ext_size;
8635 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8639 (*info->callbacks->warning)
8640 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8644 if (bed->s->arch_size == 32)
8645 r_sym_mask = ~(bfd_vma) 0xff;
8647 r_sym_mask = ~(bfd_vma) 0xffffffff;
8649 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8650 if (lo->type == bfd_indirect_link_order)
8652 bfd_byte *erel, *erelend;
8653 asection *o = lo->u.indirect.section;
8655 if (o->contents == NULL && o->size != 0)
8657 /* This is a reloc section that is being handled as a normal
8658 section. See bfd_section_from_shdr. We can't combine
8659 relocs in this case. */
8664 erelend = o->contents + o->size;
8665 /* FIXME: octets_per_byte. */
8666 p = sort + o->output_offset / ext_size * sort_elt;
8668 while (erel < erelend)
8670 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8672 (*swap_in) (abfd, erel, s->rela);
8673 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8674 s->u.sym_mask = r_sym_mask;
8680 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8682 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8684 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8685 if (s->type != reloc_class_relative)
8691 sq = (struct elf_link_sort_rela *) s_non_relative;
8692 for (; i < count; i++, p += sort_elt)
8694 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8695 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8697 sp->u.offset = sq->rela->r_offset;
8700 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8702 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8703 if (lo->type == bfd_indirect_link_order)
8705 bfd_byte *erel, *erelend;
8706 asection *o = lo->u.indirect.section;
8709 erelend = o->contents + o->size;
8710 /* FIXME: octets_per_byte. */
8711 p = sort + o->output_offset / ext_size * sort_elt;
8712 while (erel < erelend)
8714 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8715 (*swap_out) (abfd, s->rela, erel);
8722 *psec = dynamic_relocs;
8726 /* Add a symbol to the output symbol string table. */
8729 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8731 Elf_Internal_Sym *elfsym,
8732 asection *input_sec,
8733 struct elf_link_hash_entry *h)
8735 int (*output_symbol_hook)
8736 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8737 struct elf_link_hash_entry *);
8738 struct elf_link_hash_table *hash_table;
8739 const struct elf_backend_data *bed;
8740 bfd_size_type strtabsize;
8742 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8744 bed = get_elf_backend_data (flinfo->output_bfd);
8745 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8746 if (output_symbol_hook != NULL)
8748 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8755 || (input_sec->flags & SEC_EXCLUDE))
8756 elfsym->st_name = (unsigned long) -1;
8759 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8760 to get the final offset for st_name. */
8762 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8764 if (elfsym->st_name == (unsigned long) -1)
8768 hash_table = elf_hash_table (flinfo->info);
8769 strtabsize = hash_table->strtabsize;
8770 if (strtabsize <= hash_table->strtabcount)
8772 strtabsize += strtabsize;
8773 hash_table->strtabsize = strtabsize;
8774 strtabsize *= sizeof (*hash_table->strtab);
8776 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8778 if (hash_table->strtab == NULL)
8781 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8782 hash_table->strtab[hash_table->strtabcount].dest_index
8783 = hash_table->strtabcount;
8784 hash_table->strtab[hash_table->strtabcount].destshndx_index
8785 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8787 bfd_get_symcount (flinfo->output_bfd) += 1;
8788 hash_table->strtabcount += 1;
8793 /* Swap symbols out to the symbol table and flush the output symbols to
8797 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8799 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8800 bfd_size_type amt, i;
8801 const struct elf_backend_data *bed;
8803 Elf_Internal_Shdr *hdr;
8807 if (!hash_table->strtabcount)
8810 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8812 bed = get_elf_backend_data (flinfo->output_bfd);
8814 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8815 symbuf = (bfd_byte *) bfd_malloc (amt);
8819 if (flinfo->symshndxbuf)
8821 amt = (sizeof (Elf_External_Sym_Shndx)
8822 * (bfd_get_symcount (flinfo->output_bfd)));
8823 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8824 if (flinfo->symshndxbuf == NULL)
8831 for (i = 0; i < hash_table->strtabcount; i++)
8833 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8834 if (elfsym->sym.st_name == (unsigned long) -1)
8835 elfsym->sym.st_name = 0;
8838 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8839 elfsym->sym.st_name);
8840 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8841 ((bfd_byte *) symbuf
8842 + (elfsym->dest_index
8843 * bed->s->sizeof_sym)),
8844 (flinfo->symshndxbuf
8845 + elfsym->destshndx_index));
8848 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8849 pos = hdr->sh_offset + hdr->sh_size;
8850 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8851 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8852 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8854 hdr->sh_size += amt;
8862 free (hash_table->strtab);
8863 hash_table->strtab = NULL;
8868 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8871 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8873 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8874 && sym->st_shndx < SHN_LORESERVE)
8876 /* The gABI doesn't support dynamic symbols in output sections
8878 (*_bfd_error_handler)
8879 (_("%B: Too many sections: %d (>= %d)"),
8880 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8881 bfd_set_error (bfd_error_nonrepresentable_section);
8887 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8888 allowing an unsatisfied unversioned symbol in the DSO to match a
8889 versioned symbol that would normally require an explicit version.
8890 We also handle the case that a DSO references a hidden symbol
8891 which may be satisfied by a versioned symbol in another DSO. */
8894 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8895 const struct elf_backend_data *bed,
8896 struct elf_link_hash_entry *h)
8899 struct elf_link_loaded_list *loaded;
8901 if (!is_elf_hash_table (info->hash))
8904 /* Check indirect symbol. */
8905 while (h->root.type == bfd_link_hash_indirect)
8906 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8908 switch (h->root.type)
8914 case bfd_link_hash_undefined:
8915 case bfd_link_hash_undefweak:
8916 abfd = h->root.u.undef.abfd;
8917 if ((abfd->flags & DYNAMIC) == 0
8918 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8922 case bfd_link_hash_defined:
8923 case bfd_link_hash_defweak:
8924 abfd = h->root.u.def.section->owner;
8927 case bfd_link_hash_common:
8928 abfd = h->root.u.c.p->section->owner;
8931 BFD_ASSERT (abfd != NULL);
8933 for (loaded = elf_hash_table (info)->loaded;
8935 loaded = loaded->next)
8938 Elf_Internal_Shdr *hdr;
8939 bfd_size_type symcount;
8940 bfd_size_type extsymcount;
8941 bfd_size_type extsymoff;
8942 Elf_Internal_Shdr *versymhdr;
8943 Elf_Internal_Sym *isym;
8944 Elf_Internal_Sym *isymend;
8945 Elf_Internal_Sym *isymbuf;
8946 Elf_External_Versym *ever;
8947 Elf_External_Versym *extversym;
8949 input = loaded->abfd;
8951 /* We check each DSO for a possible hidden versioned definition. */
8953 || (input->flags & DYNAMIC) == 0
8954 || elf_dynversym (input) == 0)
8957 hdr = &elf_tdata (input)->dynsymtab_hdr;
8959 symcount = hdr->sh_size / bed->s->sizeof_sym;
8960 if (elf_bad_symtab (input))
8962 extsymcount = symcount;
8967 extsymcount = symcount - hdr->sh_info;
8968 extsymoff = hdr->sh_info;
8971 if (extsymcount == 0)
8974 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8976 if (isymbuf == NULL)
8979 /* Read in any version definitions. */
8980 versymhdr = &elf_tdata (input)->dynversym_hdr;
8981 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8982 if (extversym == NULL)
8985 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8986 || (bfd_bread (extversym, versymhdr->sh_size, input)
8987 != versymhdr->sh_size))
8995 ever = extversym + extsymoff;
8996 isymend = isymbuf + extsymcount;
8997 for (isym = isymbuf; isym < isymend; isym++, ever++)
9000 Elf_Internal_Versym iver;
9001 unsigned short version_index;
9003 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9004 || isym->st_shndx == SHN_UNDEF)
9007 name = bfd_elf_string_from_elf_section (input,
9010 if (strcmp (name, h->root.root.string) != 0)
9013 _bfd_elf_swap_versym_in (input, ever, &iver);
9015 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9017 && h->forced_local))
9019 /* If we have a non-hidden versioned sym, then it should
9020 have provided a definition for the undefined sym unless
9021 it is defined in a non-shared object and forced local.
9026 version_index = iver.vs_vers & VERSYM_VERSION;
9027 if (version_index == 1 || version_index == 2)
9029 /* This is the base or first version. We can use it. */
9043 /* Convert ELF common symbol TYPE. */
9046 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9048 /* Commom symbol can only appear in relocatable link. */
9049 if (!bfd_link_relocatable (info))
9051 switch (info->elf_stt_common)
9055 case elf_stt_common:
9058 case no_elf_stt_common:
9065 /* Add an external symbol to the symbol table. This is called from
9066 the hash table traversal routine. When generating a shared object,
9067 we go through the symbol table twice. The first time we output
9068 anything that might have been forced to local scope in a version
9069 script. The second time we output the symbols that are still
9073 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9075 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9076 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9077 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9079 Elf_Internal_Sym sym;
9080 asection *input_sec;
9081 const struct elf_backend_data *bed;
9085 /* A symbol is bound locally if it is forced local or it is locally
9086 defined, hidden versioned, not referenced by shared library and
9087 not exported when linking executable. */
9088 bfd_boolean local_bind = (h->forced_local
9089 || (bfd_link_executable (flinfo->info)
9090 && !flinfo->info->export_dynamic
9094 && h->versioned == versioned_hidden));
9096 if (h->root.type == bfd_link_hash_warning)
9098 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9099 if (h->root.type == bfd_link_hash_new)
9103 /* Decide whether to output this symbol in this pass. */
9104 if (eoinfo->localsyms)
9115 bed = get_elf_backend_data (flinfo->output_bfd);
9117 if (h->root.type == bfd_link_hash_undefined)
9119 /* If we have an undefined symbol reference here then it must have
9120 come from a shared library that is being linked in. (Undefined
9121 references in regular files have already been handled unless
9122 they are in unreferenced sections which are removed by garbage
9124 bfd_boolean ignore_undef = FALSE;
9126 /* Some symbols may be special in that the fact that they're
9127 undefined can be safely ignored - let backend determine that. */
9128 if (bed->elf_backend_ignore_undef_symbol)
9129 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9131 /* If we are reporting errors for this situation then do so now. */
9134 && (!h->ref_regular || flinfo->info->gc_sections)
9135 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9136 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9138 if (!(flinfo->info->callbacks->undefined_symbol
9139 (flinfo->info, h->root.root.string,
9140 h->ref_regular ? NULL : h->root.u.undef.abfd,
9142 (flinfo->info->unresolved_syms_in_shared_libs
9143 == RM_GENERATE_ERROR))))
9145 bfd_set_error (bfd_error_bad_value);
9146 eoinfo->failed = TRUE;
9152 /* We should also warn if a forced local symbol is referenced from
9153 shared libraries. */
9154 if (bfd_link_executable (flinfo->info)
9159 && h->ref_dynamic_nonweak
9160 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9164 struct elf_link_hash_entry *hi = h;
9166 /* Check indirect symbol. */
9167 while (hi->root.type == bfd_link_hash_indirect)
9168 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9170 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9171 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9172 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9173 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9175 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9176 def_bfd = flinfo->output_bfd;
9177 if (hi->root.u.def.section != bfd_abs_section_ptr)
9178 def_bfd = hi->root.u.def.section->owner;
9179 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9180 h->root.root.string);
9181 bfd_set_error (bfd_error_bad_value);
9182 eoinfo->failed = TRUE;
9186 /* We don't want to output symbols that have never been mentioned by
9187 a regular file, or that we have been told to strip. However, if
9188 h->indx is set to -2, the symbol is used by a reloc and we must
9193 else if ((h->def_dynamic
9195 || h->root.type == bfd_link_hash_new)
9199 else if (flinfo->info->strip == strip_all)
9201 else if (flinfo->info->strip == strip_some
9202 && bfd_hash_lookup (flinfo->info->keep_hash,
9203 h->root.root.string, FALSE, FALSE) == NULL)
9205 else if ((h->root.type == bfd_link_hash_defined
9206 || h->root.type == bfd_link_hash_defweak)
9207 && ((flinfo->info->strip_discarded
9208 && discarded_section (h->root.u.def.section))
9209 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9210 && h->root.u.def.section->owner != NULL
9211 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9213 else if ((h->root.type == bfd_link_hash_undefined
9214 || h->root.type == bfd_link_hash_undefweak)
9215 && h->root.u.undef.abfd != NULL
9216 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9221 /* If we're stripping it, and it's not a dynamic symbol, there's
9222 nothing else to do. However, if it is a forced local symbol or
9223 an ifunc symbol we need to give the backend finish_dynamic_symbol
9224 function a chance to make it dynamic. */
9227 && type != STT_GNU_IFUNC
9228 && !h->forced_local)
9232 sym.st_size = h->size;
9233 sym.st_other = h->other;
9234 switch (h->root.type)
9237 case bfd_link_hash_new:
9238 case bfd_link_hash_warning:
9242 case bfd_link_hash_undefined:
9243 case bfd_link_hash_undefweak:
9244 input_sec = bfd_und_section_ptr;
9245 sym.st_shndx = SHN_UNDEF;
9248 case bfd_link_hash_defined:
9249 case bfd_link_hash_defweak:
9251 input_sec = h->root.u.def.section;
9252 if (input_sec->output_section != NULL)
9255 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9256 input_sec->output_section);
9257 if (sym.st_shndx == SHN_BAD)
9259 (*_bfd_error_handler)
9260 (_("%B: could not find output section %A for input section %A"),
9261 flinfo->output_bfd, input_sec->output_section, input_sec);
9262 bfd_set_error (bfd_error_nonrepresentable_section);
9263 eoinfo->failed = TRUE;
9267 /* ELF symbols in relocatable files are section relative,
9268 but in nonrelocatable files they are virtual
9270 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9271 if (!bfd_link_relocatable (flinfo->info))
9273 sym.st_value += input_sec->output_section->vma;
9274 if (h->type == STT_TLS)
9276 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9277 if (tls_sec != NULL)
9278 sym.st_value -= tls_sec->vma;
9284 BFD_ASSERT (input_sec->owner == NULL
9285 || (input_sec->owner->flags & DYNAMIC) != 0);
9286 sym.st_shndx = SHN_UNDEF;
9287 input_sec = bfd_und_section_ptr;
9292 case bfd_link_hash_common:
9293 input_sec = h->root.u.c.p->section;
9294 sym.st_shndx = bed->common_section_index (input_sec);
9295 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9298 case bfd_link_hash_indirect:
9299 /* These symbols are created by symbol versioning. They point
9300 to the decorated version of the name. For example, if the
9301 symbol foo@@GNU_1.2 is the default, which should be used when
9302 foo is used with no version, then we add an indirect symbol
9303 foo which points to foo@@GNU_1.2. We ignore these symbols,
9304 since the indirected symbol is already in the hash table. */
9308 if (type == STT_COMMON || type == STT_OBJECT)
9309 switch (h->root.type)
9311 case bfd_link_hash_common:
9312 type = elf_link_convert_common_type (flinfo->info, type);
9314 case bfd_link_hash_defined:
9315 case bfd_link_hash_defweak:
9316 if (bed->common_definition (&sym))
9317 type = elf_link_convert_common_type (flinfo->info, type);
9321 case bfd_link_hash_undefined:
9322 case bfd_link_hash_undefweak:
9330 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9331 /* Turn off visibility on local symbol. */
9332 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9334 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9335 else if (h->unique_global && h->def_regular)
9336 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9337 else if (h->root.type == bfd_link_hash_undefweak
9338 || h->root.type == bfd_link_hash_defweak)
9339 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9341 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9342 sym.st_target_internal = h->target_internal;
9344 /* Give the processor backend a chance to tweak the symbol value,
9345 and also to finish up anything that needs to be done for this
9346 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9347 forced local syms when non-shared is due to a historical quirk.
9348 STT_GNU_IFUNC symbol must go through PLT. */
9349 if ((h->type == STT_GNU_IFUNC
9351 && !bfd_link_relocatable (flinfo->info))
9352 || ((h->dynindx != -1
9354 && ((bfd_link_pic (flinfo->info)
9355 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9356 || h->root.type != bfd_link_hash_undefweak))
9357 || !h->forced_local)
9358 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9360 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9361 (flinfo->output_bfd, flinfo->info, h, &sym)))
9363 eoinfo->failed = TRUE;
9368 /* If we are marking the symbol as undefined, and there are no
9369 non-weak references to this symbol from a regular object, then
9370 mark the symbol as weak undefined; if there are non-weak
9371 references, mark the symbol as strong. We can't do this earlier,
9372 because it might not be marked as undefined until the
9373 finish_dynamic_symbol routine gets through with it. */
9374 if (sym.st_shndx == SHN_UNDEF
9376 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9377 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9380 type = ELF_ST_TYPE (sym.st_info);
9382 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9383 if (type == STT_GNU_IFUNC)
9386 if (h->ref_regular_nonweak)
9387 bindtype = STB_GLOBAL;
9389 bindtype = STB_WEAK;
9390 sym.st_info = ELF_ST_INFO (bindtype, type);
9393 /* If this is a symbol defined in a dynamic library, don't use the
9394 symbol size from the dynamic library. Relinking an executable
9395 against a new library may introduce gratuitous changes in the
9396 executable's symbols if we keep the size. */
9397 if (sym.st_shndx == SHN_UNDEF
9402 /* If a non-weak symbol with non-default visibility is not defined
9403 locally, it is a fatal error. */
9404 if (!bfd_link_relocatable (flinfo->info)
9405 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9406 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9407 && h->root.type == bfd_link_hash_undefined
9412 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9413 msg = _("%B: protected symbol `%s' isn't defined");
9414 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9415 msg = _("%B: internal symbol `%s' isn't defined");
9417 msg = _("%B: hidden symbol `%s' isn't defined");
9418 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9419 bfd_set_error (bfd_error_bad_value);
9420 eoinfo->failed = TRUE;
9424 /* If this symbol should be put in the .dynsym section, then put it
9425 there now. We already know the symbol index. We also fill in
9426 the entry in the .hash section. */
9427 if (elf_hash_table (flinfo->info)->dynsym != NULL
9429 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9433 /* Since there is no version information in the dynamic string,
9434 if there is no version info in symbol version section, we will
9435 have a run-time problem if not linking executable, referenced
9436 by shared library, not locally defined, or not bound locally.
9438 if (h->verinfo.verdef == NULL
9440 && (!bfd_link_executable (flinfo->info)
9442 || !h->def_regular))
9444 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9446 if (p && p [1] != '\0')
9448 (*_bfd_error_handler)
9449 (_("%B: No symbol version section for versioned symbol `%s'"),
9450 flinfo->output_bfd, h->root.root.string);
9451 eoinfo->failed = TRUE;
9456 sym.st_name = h->dynstr_index;
9457 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9458 + h->dynindx * bed->s->sizeof_sym);
9459 if (!check_dynsym (flinfo->output_bfd, &sym))
9461 eoinfo->failed = TRUE;
9464 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9466 if (flinfo->hash_sec != NULL)
9468 size_t hash_entry_size;
9469 bfd_byte *bucketpos;
9474 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9475 bucket = h->u.elf_hash_value % bucketcount;
9478 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9479 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9480 + (bucket + 2) * hash_entry_size);
9481 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9482 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9484 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9485 ((bfd_byte *) flinfo->hash_sec->contents
9486 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9489 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9491 Elf_Internal_Versym iversym;
9492 Elf_External_Versym *eversym;
9494 if (!h->def_regular)
9496 if (h->verinfo.verdef == NULL
9497 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9498 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9499 iversym.vs_vers = 0;
9501 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9505 if (h->verinfo.vertree == NULL)
9506 iversym.vs_vers = 1;
9508 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9509 if (flinfo->info->create_default_symver)
9513 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9515 if (h->versioned == versioned_hidden && h->def_regular)
9516 iversym.vs_vers |= VERSYM_HIDDEN;
9518 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9519 eversym += h->dynindx;
9520 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9524 /* If the symbol is undefined, and we didn't output it to .dynsym,
9525 strip it from .symtab too. Obviously we can't do this for
9526 relocatable output or when needed for --emit-relocs. */
9527 else if (input_sec == bfd_und_section_ptr
9529 && !bfd_link_relocatable (flinfo->info))
9531 /* Also strip others that we couldn't earlier due to dynamic symbol
9535 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9538 /* Output a FILE symbol so that following locals are not associated
9539 with the wrong input file. We need one for forced local symbols
9540 if we've seen more than one FILE symbol or when we have exactly
9541 one FILE symbol but global symbols are present in a file other
9542 than the one with the FILE symbol. We also need one if linker
9543 defined symbols are present. In practice these conditions are
9544 always met, so just emit the FILE symbol unconditionally. */
9545 if (eoinfo->localsyms
9546 && !eoinfo->file_sym_done
9547 && eoinfo->flinfo->filesym_count != 0)
9549 Elf_Internal_Sym fsym;
9551 memset (&fsym, 0, sizeof (fsym));
9552 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9553 fsym.st_shndx = SHN_ABS;
9554 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9555 bfd_und_section_ptr, NULL))
9558 eoinfo->file_sym_done = TRUE;
9561 indx = bfd_get_symcount (flinfo->output_bfd);
9562 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9566 eoinfo->failed = TRUE;
9571 else if (h->indx == -2)
9577 /* Return TRUE if special handling is done for relocs in SEC against
9578 symbols defined in discarded sections. */
9581 elf_section_ignore_discarded_relocs (asection *sec)
9583 const struct elf_backend_data *bed;
9585 switch (sec->sec_info_type)
9587 case SEC_INFO_TYPE_STABS:
9588 case SEC_INFO_TYPE_EH_FRAME:
9589 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9595 bed = get_elf_backend_data (sec->owner);
9596 if (bed->elf_backend_ignore_discarded_relocs != NULL
9597 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9603 /* Return a mask saying how ld should treat relocations in SEC against
9604 symbols defined in discarded sections. If this function returns
9605 COMPLAIN set, ld will issue a warning message. If this function
9606 returns PRETEND set, and the discarded section was link-once and the
9607 same size as the kept link-once section, ld will pretend that the
9608 symbol was actually defined in the kept section. Otherwise ld will
9609 zero the reloc (at least that is the intent, but some cooperation by
9610 the target dependent code is needed, particularly for REL targets). */
9613 _bfd_elf_default_action_discarded (asection *sec)
9615 if (sec->flags & SEC_DEBUGGING)
9618 if (strcmp (".eh_frame", sec->name) == 0)
9621 if (strcmp (".gcc_except_table", sec->name) == 0)
9624 return COMPLAIN | PRETEND;
9627 /* Find a match between a section and a member of a section group. */
9630 match_group_member (asection *sec, asection *group,
9631 struct bfd_link_info *info)
9633 asection *first = elf_next_in_group (group);
9634 asection *s = first;
9638 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9641 s = elf_next_in_group (s);
9649 /* Check if the kept section of a discarded section SEC can be used
9650 to replace it. Return the replacement if it is OK. Otherwise return
9654 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9658 kept = sec->kept_section;
9661 if ((kept->flags & SEC_GROUP) != 0)
9662 kept = match_group_member (sec, kept, info);
9664 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9665 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9667 sec->kept_section = kept;
9672 /* Link an input file into the linker output file. This function
9673 handles all the sections and relocations of the input file at once.
9674 This is so that we only have to read the local symbols once, and
9675 don't have to keep them in memory. */
9678 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9680 int (*relocate_section)
9681 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9682 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9684 Elf_Internal_Shdr *symtab_hdr;
9687 Elf_Internal_Sym *isymbuf;
9688 Elf_Internal_Sym *isym;
9689 Elf_Internal_Sym *isymend;
9691 asection **ppsection;
9693 const struct elf_backend_data *bed;
9694 struct elf_link_hash_entry **sym_hashes;
9695 bfd_size_type address_size;
9696 bfd_vma r_type_mask;
9698 bfd_boolean have_file_sym = FALSE;
9700 output_bfd = flinfo->output_bfd;
9701 bed = get_elf_backend_data (output_bfd);
9702 relocate_section = bed->elf_backend_relocate_section;
9704 /* If this is a dynamic object, we don't want to do anything here:
9705 we don't want the local symbols, and we don't want the section
9707 if ((input_bfd->flags & DYNAMIC) != 0)
9710 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9711 if (elf_bad_symtab (input_bfd))
9713 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9718 locsymcount = symtab_hdr->sh_info;
9719 extsymoff = symtab_hdr->sh_info;
9722 /* Read the local symbols. */
9723 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9724 if (isymbuf == NULL && locsymcount != 0)
9726 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9727 flinfo->internal_syms,
9728 flinfo->external_syms,
9729 flinfo->locsym_shndx);
9730 if (isymbuf == NULL)
9734 /* Find local symbol sections and adjust values of symbols in
9735 SEC_MERGE sections. Write out those local symbols we know are
9736 going into the output file. */
9737 isymend = isymbuf + locsymcount;
9738 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9740 isym++, pindex++, ppsection++)
9744 Elf_Internal_Sym osym;
9750 if (elf_bad_symtab (input_bfd))
9752 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9759 if (isym->st_shndx == SHN_UNDEF)
9760 isec = bfd_und_section_ptr;
9761 else if (isym->st_shndx == SHN_ABS)
9762 isec = bfd_abs_section_ptr;
9763 else if (isym->st_shndx == SHN_COMMON)
9764 isec = bfd_com_section_ptr;
9767 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9770 /* Don't attempt to output symbols with st_shnx in the
9771 reserved range other than SHN_ABS and SHN_COMMON. */
9775 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9776 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9778 _bfd_merged_section_offset (output_bfd, &isec,
9779 elf_section_data (isec)->sec_info,
9785 /* Don't output the first, undefined, symbol. In fact, don't
9786 output any undefined local symbol. */
9787 if (isec == bfd_und_section_ptr)
9790 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9792 /* We never output section symbols. Instead, we use the
9793 section symbol of the corresponding section in the output
9798 /* If we are stripping all symbols, we don't want to output this
9800 if (flinfo->info->strip == strip_all)
9803 /* If we are discarding all local symbols, we don't want to
9804 output this one. If we are generating a relocatable output
9805 file, then some of the local symbols may be required by
9806 relocs; we output them below as we discover that they are
9808 if (flinfo->info->discard == discard_all)
9811 /* If this symbol is defined in a section which we are
9812 discarding, we don't need to keep it. */
9813 if (isym->st_shndx != SHN_UNDEF
9814 && isym->st_shndx < SHN_LORESERVE
9815 && bfd_section_removed_from_list (output_bfd,
9816 isec->output_section))
9819 /* Get the name of the symbol. */
9820 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9825 /* See if we are discarding symbols with this name. */
9826 if ((flinfo->info->strip == strip_some
9827 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9829 || (((flinfo->info->discard == discard_sec_merge
9830 && (isec->flags & SEC_MERGE)
9831 && !bfd_link_relocatable (flinfo->info))
9832 || flinfo->info->discard == discard_l)
9833 && bfd_is_local_label_name (input_bfd, name)))
9836 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9838 if (input_bfd->lto_output)
9839 /* -flto puts a temp file name here. This means builds
9840 are not reproducible. Discard the symbol. */
9842 have_file_sym = TRUE;
9843 flinfo->filesym_count += 1;
9847 /* In the absence of debug info, bfd_find_nearest_line uses
9848 FILE symbols to determine the source file for local
9849 function symbols. Provide a FILE symbol here if input
9850 files lack such, so that their symbols won't be
9851 associated with a previous input file. It's not the
9852 source file, but the best we can do. */
9853 have_file_sym = TRUE;
9854 flinfo->filesym_count += 1;
9855 memset (&osym, 0, sizeof (osym));
9856 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9857 osym.st_shndx = SHN_ABS;
9858 if (!elf_link_output_symstrtab (flinfo,
9859 (input_bfd->lto_output ? NULL
9860 : input_bfd->filename),
9861 &osym, bfd_abs_section_ptr,
9868 /* Adjust the section index for the output file. */
9869 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9870 isec->output_section);
9871 if (osym.st_shndx == SHN_BAD)
9874 /* ELF symbols in relocatable files are section relative, but
9875 in executable files they are virtual addresses. Note that
9876 this code assumes that all ELF sections have an associated
9877 BFD section with a reasonable value for output_offset; below
9878 we assume that they also have a reasonable value for
9879 output_section. Any special sections must be set up to meet
9880 these requirements. */
9881 osym.st_value += isec->output_offset;
9882 if (!bfd_link_relocatable (flinfo->info))
9884 osym.st_value += isec->output_section->vma;
9885 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9887 /* STT_TLS symbols are relative to PT_TLS segment base. */
9888 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9889 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9893 indx = bfd_get_symcount (output_bfd);
9894 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9901 if (bed->s->arch_size == 32)
9909 r_type_mask = 0xffffffff;
9914 /* Relocate the contents of each section. */
9915 sym_hashes = elf_sym_hashes (input_bfd);
9916 for (o = input_bfd->sections; o != NULL; o = o->next)
9920 if (! o->linker_mark)
9922 /* This section was omitted from the link. */
9926 if (bfd_link_relocatable (flinfo->info)
9927 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9929 /* Deal with the group signature symbol. */
9930 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9931 unsigned long symndx = sec_data->this_hdr.sh_info;
9932 asection *osec = o->output_section;
9934 if (symndx >= locsymcount
9935 || (elf_bad_symtab (input_bfd)
9936 && flinfo->sections[symndx] == NULL))
9938 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9939 while (h->root.type == bfd_link_hash_indirect
9940 || h->root.type == bfd_link_hash_warning)
9941 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9942 /* Arrange for symbol to be output. */
9944 elf_section_data (osec)->this_hdr.sh_info = -2;
9946 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9948 /* We'll use the output section target_index. */
9949 asection *sec = flinfo->sections[symndx]->output_section;
9950 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9954 if (flinfo->indices[symndx] == -1)
9956 /* Otherwise output the local symbol now. */
9957 Elf_Internal_Sym sym = isymbuf[symndx];
9958 asection *sec = flinfo->sections[symndx]->output_section;
9963 name = bfd_elf_string_from_elf_section (input_bfd,
9964 symtab_hdr->sh_link,
9969 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9971 if (sym.st_shndx == SHN_BAD)
9974 sym.st_value += o->output_offset;
9976 indx = bfd_get_symcount (output_bfd);
9977 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9982 flinfo->indices[symndx] = indx;
9986 elf_section_data (osec)->this_hdr.sh_info
9987 = flinfo->indices[symndx];
9991 if ((o->flags & SEC_HAS_CONTENTS) == 0
9992 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9995 if ((o->flags & SEC_LINKER_CREATED) != 0)
9997 /* Section was created by _bfd_elf_link_create_dynamic_sections
10002 /* Get the contents of the section. They have been cached by a
10003 relaxation routine. Note that o is a section in an input
10004 file, so the contents field will not have been set by any of
10005 the routines which work on output files. */
10006 if (elf_section_data (o)->this_hdr.contents != NULL)
10008 contents = elf_section_data (o)->this_hdr.contents;
10009 if (bed->caches_rawsize
10011 && o->rawsize < o->size)
10013 memcpy (flinfo->contents, contents, o->rawsize);
10014 contents = flinfo->contents;
10019 contents = flinfo->contents;
10020 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10024 if ((o->flags & SEC_RELOC) != 0)
10026 Elf_Internal_Rela *internal_relocs;
10027 Elf_Internal_Rela *rel, *relend;
10028 int action_discarded;
10031 /* Get the swapped relocs. */
10033 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10034 flinfo->internal_relocs, FALSE);
10035 if (internal_relocs == NULL
10036 && o->reloc_count > 0)
10039 /* We need to reverse-copy input .ctors/.dtors sections if
10040 they are placed in .init_array/.finit_array for output. */
10041 if (o->size > address_size
10042 && ((strncmp (o->name, ".ctors", 6) == 0
10043 && strcmp (o->output_section->name,
10044 ".init_array") == 0)
10045 || (strncmp (o->name, ".dtors", 6) == 0
10046 && strcmp (o->output_section->name,
10047 ".fini_array") == 0))
10048 && (o->name[6] == 0 || o->name[6] == '.'))
10050 if (o->size != o->reloc_count * address_size)
10052 (*_bfd_error_handler)
10053 (_("error: %B: size of section %A is not "
10054 "multiple of address size"),
10056 bfd_set_error (bfd_error_on_input);
10059 o->flags |= SEC_ELF_REVERSE_COPY;
10062 action_discarded = -1;
10063 if (!elf_section_ignore_discarded_relocs (o))
10064 action_discarded = (*bed->action_discarded) (o);
10066 /* Run through the relocs evaluating complex reloc symbols and
10067 looking for relocs against symbols from discarded sections
10068 or section symbols from removed link-once sections.
10069 Complain about relocs against discarded sections. Zero
10070 relocs against removed link-once sections. */
10072 rel = internal_relocs;
10073 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10074 for ( ; rel < relend; rel++)
10076 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10077 unsigned int s_type;
10078 asection **ps, *sec;
10079 struct elf_link_hash_entry *h = NULL;
10080 const char *sym_name;
10082 if (r_symndx == STN_UNDEF)
10085 if (r_symndx >= locsymcount
10086 || (elf_bad_symtab (input_bfd)
10087 && flinfo->sections[r_symndx] == NULL))
10089 h = sym_hashes[r_symndx - extsymoff];
10091 /* Badly formatted input files can contain relocs that
10092 reference non-existant symbols. Check here so that
10093 we do not seg fault. */
10098 sprintf_vma (buffer, rel->r_info);
10099 (*_bfd_error_handler)
10100 (_("error: %B contains a reloc (0x%s) for section %A "
10101 "that references a non-existent global symbol"),
10102 input_bfd, o, buffer);
10103 bfd_set_error (bfd_error_bad_value);
10107 while (h->root.type == bfd_link_hash_indirect
10108 || h->root.type == bfd_link_hash_warning)
10109 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10113 /* If a plugin symbol is referenced from a non-IR file,
10114 mark the symbol as undefined. Note that the
10115 linker may attach linker created dynamic sections
10116 to the plugin bfd. Symbols defined in linker
10117 created sections are not plugin symbols. */
10118 if (h->root.non_ir_ref
10119 && (h->root.type == bfd_link_hash_defined
10120 || h->root.type == bfd_link_hash_defweak)
10121 && (h->root.u.def.section->flags
10122 & SEC_LINKER_CREATED) == 0
10123 && h->root.u.def.section->owner != NULL
10124 && (h->root.u.def.section->owner->flags
10125 & BFD_PLUGIN) != 0)
10127 h->root.type = bfd_link_hash_undefined;
10128 h->root.u.undef.abfd = h->root.u.def.section->owner;
10132 if (h->root.type == bfd_link_hash_defined
10133 || h->root.type == bfd_link_hash_defweak)
10134 ps = &h->root.u.def.section;
10136 sym_name = h->root.root.string;
10140 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10142 s_type = ELF_ST_TYPE (sym->st_info);
10143 ps = &flinfo->sections[r_symndx];
10144 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10148 if ((s_type == STT_RELC || s_type == STT_SRELC)
10149 && !bfd_link_relocatable (flinfo->info))
10152 bfd_vma dot = (rel->r_offset
10153 + o->output_offset + o->output_section->vma);
10155 printf ("Encountered a complex symbol!");
10156 printf (" (input_bfd %s, section %s, reloc %ld\n",
10157 input_bfd->filename, o->name,
10158 (long) (rel - internal_relocs));
10159 printf (" symbol: idx %8.8lx, name %s\n",
10160 r_symndx, sym_name);
10161 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10162 (unsigned long) rel->r_info,
10163 (unsigned long) rel->r_offset);
10165 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10166 isymbuf, locsymcount, s_type == STT_SRELC))
10169 /* Symbol evaluated OK. Update to absolute value. */
10170 set_symbol_value (input_bfd, isymbuf, locsymcount,
10175 if (action_discarded != -1 && ps != NULL)
10177 /* Complain if the definition comes from a
10178 discarded section. */
10179 if ((sec = *ps) != NULL && discarded_section (sec))
10181 BFD_ASSERT (r_symndx != STN_UNDEF);
10182 if (action_discarded & COMPLAIN)
10183 (*flinfo->info->callbacks->einfo)
10184 (_("%X`%s' referenced in section `%A' of %B: "
10185 "defined in discarded section `%A' of %B\n"),
10186 sym_name, o, input_bfd, sec, sec->owner);
10188 /* Try to do the best we can to support buggy old
10189 versions of gcc. Pretend that the symbol is
10190 really defined in the kept linkonce section.
10191 FIXME: This is quite broken. Modifying the
10192 symbol here means we will be changing all later
10193 uses of the symbol, not just in this section. */
10194 if (action_discarded & PRETEND)
10198 kept = _bfd_elf_check_kept_section (sec,
10210 /* Relocate the section by invoking a back end routine.
10212 The back end routine is responsible for adjusting the
10213 section contents as necessary, and (if using Rela relocs
10214 and generating a relocatable output file) adjusting the
10215 reloc addend as necessary.
10217 The back end routine does not have to worry about setting
10218 the reloc address or the reloc symbol index.
10220 The back end routine is given a pointer to the swapped in
10221 internal symbols, and can access the hash table entries
10222 for the external symbols via elf_sym_hashes (input_bfd).
10224 When generating relocatable output, the back end routine
10225 must handle STB_LOCAL/STT_SECTION symbols specially. The
10226 output symbol is going to be a section symbol
10227 corresponding to the output section, which will require
10228 the addend to be adjusted. */
10230 ret = (*relocate_section) (output_bfd, flinfo->info,
10231 input_bfd, o, contents,
10239 || bfd_link_relocatable (flinfo->info)
10240 || flinfo->info->emitrelocations)
10242 Elf_Internal_Rela *irela;
10243 Elf_Internal_Rela *irelaend, *irelamid;
10244 bfd_vma last_offset;
10245 struct elf_link_hash_entry **rel_hash;
10246 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10247 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10248 unsigned int next_erel;
10249 bfd_boolean rela_normal;
10250 struct bfd_elf_section_data *esdi, *esdo;
10252 esdi = elf_section_data (o);
10253 esdo = elf_section_data (o->output_section);
10254 rela_normal = FALSE;
10256 /* Adjust the reloc addresses and symbol indices. */
10258 irela = internal_relocs;
10259 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10260 rel_hash = esdo->rel.hashes + esdo->rel.count;
10261 /* We start processing the REL relocs, if any. When we reach
10262 IRELAMID in the loop, we switch to the RELA relocs. */
10264 if (esdi->rel.hdr != NULL)
10265 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10266 * bed->s->int_rels_per_ext_rel);
10267 rel_hash_list = rel_hash;
10268 rela_hash_list = NULL;
10269 last_offset = o->output_offset;
10270 if (!bfd_link_relocatable (flinfo->info))
10271 last_offset += o->output_section->vma;
10272 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10274 unsigned long r_symndx;
10276 Elf_Internal_Sym sym;
10278 if (next_erel == bed->s->int_rels_per_ext_rel)
10284 if (irela == irelamid)
10286 rel_hash = esdo->rela.hashes + esdo->rela.count;
10287 rela_hash_list = rel_hash;
10288 rela_normal = bed->rela_normal;
10291 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10294 if (irela->r_offset >= (bfd_vma) -2)
10296 /* This is a reloc for a deleted entry or somesuch.
10297 Turn it into an R_*_NONE reloc, at the same
10298 offset as the last reloc. elf_eh_frame.c and
10299 bfd_elf_discard_info rely on reloc offsets
10301 irela->r_offset = last_offset;
10303 irela->r_addend = 0;
10307 irela->r_offset += o->output_offset;
10309 /* Relocs in an executable have to be virtual addresses. */
10310 if (!bfd_link_relocatable (flinfo->info))
10311 irela->r_offset += o->output_section->vma;
10313 last_offset = irela->r_offset;
10315 r_symndx = irela->r_info >> r_sym_shift;
10316 if (r_symndx == STN_UNDEF)
10319 if (r_symndx >= locsymcount
10320 || (elf_bad_symtab (input_bfd)
10321 && flinfo->sections[r_symndx] == NULL))
10323 struct elf_link_hash_entry *rh;
10324 unsigned long indx;
10326 /* This is a reloc against a global symbol. We
10327 have not yet output all the local symbols, so
10328 we do not know the symbol index of any global
10329 symbol. We set the rel_hash entry for this
10330 reloc to point to the global hash table entry
10331 for this symbol. The symbol index is then
10332 set at the end of bfd_elf_final_link. */
10333 indx = r_symndx - extsymoff;
10334 rh = elf_sym_hashes (input_bfd)[indx];
10335 while (rh->root.type == bfd_link_hash_indirect
10336 || rh->root.type == bfd_link_hash_warning)
10337 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10339 /* Setting the index to -2 tells
10340 elf_link_output_extsym that this symbol is
10341 used by a reloc. */
10342 BFD_ASSERT (rh->indx < 0);
10350 /* This is a reloc against a local symbol. */
10353 sym = isymbuf[r_symndx];
10354 sec = flinfo->sections[r_symndx];
10355 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10357 /* I suppose the backend ought to fill in the
10358 section of any STT_SECTION symbol against a
10359 processor specific section. */
10360 r_symndx = STN_UNDEF;
10361 if (bfd_is_abs_section (sec))
10363 else if (sec == NULL || sec->owner == NULL)
10365 bfd_set_error (bfd_error_bad_value);
10370 asection *osec = sec->output_section;
10372 /* If we have discarded a section, the output
10373 section will be the absolute section. In
10374 case of discarded SEC_MERGE sections, use
10375 the kept section. relocate_section should
10376 have already handled discarded linkonce
10378 if (bfd_is_abs_section (osec)
10379 && sec->kept_section != NULL
10380 && sec->kept_section->output_section != NULL)
10382 osec = sec->kept_section->output_section;
10383 irela->r_addend -= osec->vma;
10386 if (!bfd_is_abs_section (osec))
10388 r_symndx = osec->target_index;
10389 if (r_symndx == STN_UNDEF)
10391 irela->r_addend += osec->vma;
10392 osec = _bfd_nearby_section (output_bfd, osec,
10394 irela->r_addend -= osec->vma;
10395 r_symndx = osec->target_index;
10400 /* Adjust the addend according to where the
10401 section winds up in the output section. */
10403 irela->r_addend += sec->output_offset;
10407 if (flinfo->indices[r_symndx] == -1)
10409 unsigned long shlink;
10414 if (flinfo->info->strip == strip_all)
10416 /* You can't do ld -r -s. */
10417 bfd_set_error (bfd_error_invalid_operation);
10421 /* This symbol was skipped earlier, but
10422 since it is needed by a reloc, we
10423 must output it now. */
10424 shlink = symtab_hdr->sh_link;
10425 name = (bfd_elf_string_from_elf_section
10426 (input_bfd, shlink, sym.st_name));
10430 osec = sec->output_section;
10432 _bfd_elf_section_from_bfd_section (output_bfd,
10434 if (sym.st_shndx == SHN_BAD)
10437 sym.st_value += sec->output_offset;
10438 if (!bfd_link_relocatable (flinfo->info))
10440 sym.st_value += osec->vma;
10441 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10443 /* STT_TLS symbols are relative to PT_TLS
10445 BFD_ASSERT (elf_hash_table (flinfo->info)
10446 ->tls_sec != NULL);
10447 sym.st_value -= (elf_hash_table (flinfo->info)
10452 indx = bfd_get_symcount (output_bfd);
10453 ret = elf_link_output_symstrtab (flinfo, name,
10459 flinfo->indices[r_symndx] = indx;
10464 r_symndx = flinfo->indices[r_symndx];
10467 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10468 | (irela->r_info & r_type_mask));
10471 /* Swap out the relocs. */
10472 input_rel_hdr = esdi->rel.hdr;
10473 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10475 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10480 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10481 * bed->s->int_rels_per_ext_rel);
10482 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10485 input_rela_hdr = esdi->rela.hdr;
10486 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10488 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10497 /* Write out the modified section contents. */
10498 if (bed->elf_backend_write_section
10499 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10502 /* Section written out. */
10504 else switch (o->sec_info_type)
10506 case SEC_INFO_TYPE_STABS:
10507 if (! (_bfd_write_section_stabs
10509 &elf_hash_table (flinfo->info)->stab_info,
10510 o, &elf_section_data (o)->sec_info, contents)))
10513 case SEC_INFO_TYPE_MERGE:
10514 if (! _bfd_write_merged_section (output_bfd, o,
10515 elf_section_data (o)->sec_info))
10518 case SEC_INFO_TYPE_EH_FRAME:
10520 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10525 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10527 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10535 /* FIXME: octets_per_byte. */
10536 if (! (o->flags & SEC_EXCLUDE))
10538 file_ptr offset = (file_ptr) o->output_offset;
10539 bfd_size_type todo = o->size;
10540 if ((o->flags & SEC_ELF_REVERSE_COPY))
10542 /* Reverse-copy input section to output. */
10545 todo -= address_size;
10546 if (! bfd_set_section_contents (output_bfd,
10554 offset += address_size;
10558 else if (! bfd_set_section_contents (output_bfd,
10572 /* Generate a reloc when linking an ELF file. This is a reloc
10573 requested by the linker, and does not come from any input file. This
10574 is used to build constructor and destructor tables when linking
10578 elf_reloc_link_order (bfd *output_bfd,
10579 struct bfd_link_info *info,
10580 asection *output_section,
10581 struct bfd_link_order *link_order)
10583 reloc_howto_type *howto;
10587 struct bfd_elf_section_reloc_data *reldata;
10588 struct elf_link_hash_entry **rel_hash_ptr;
10589 Elf_Internal_Shdr *rel_hdr;
10590 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10591 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10594 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10596 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10599 bfd_set_error (bfd_error_bad_value);
10603 addend = link_order->u.reloc.p->addend;
10606 reldata = &esdo->rel;
10607 else if (esdo->rela.hdr)
10608 reldata = &esdo->rela;
10615 /* Figure out the symbol index. */
10616 rel_hash_ptr = reldata->hashes + reldata->count;
10617 if (link_order->type == bfd_section_reloc_link_order)
10619 indx = link_order->u.reloc.p->u.section->target_index;
10620 BFD_ASSERT (indx != 0);
10621 *rel_hash_ptr = NULL;
10625 struct elf_link_hash_entry *h;
10627 /* Treat a reloc against a defined symbol as though it were
10628 actually against the section. */
10629 h = ((struct elf_link_hash_entry *)
10630 bfd_wrapped_link_hash_lookup (output_bfd, info,
10631 link_order->u.reloc.p->u.name,
10632 FALSE, FALSE, TRUE));
10634 && (h->root.type == bfd_link_hash_defined
10635 || h->root.type == bfd_link_hash_defweak))
10639 section = h->root.u.def.section;
10640 indx = section->output_section->target_index;
10641 *rel_hash_ptr = NULL;
10642 /* It seems that we ought to add the symbol value to the
10643 addend here, but in practice it has already been added
10644 because it was passed to constructor_callback. */
10645 addend += section->output_section->vma + section->output_offset;
10647 else if (h != NULL)
10649 /* Setting the index to -2 tells elf_link_output_extsym that
10650 this symbol is used by a reloc. */
10657 if (! ((*info->callbacks->unattached_reloc)
10658 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10664 /* If this is an inplace reloc, we must write the addend into the
10666 if (howto->partial_inplace && addend != 0)
10668 bfd_size_type size;
10669 bfd_reloc_status_type rstat;
10672 const char *sym_name;
10674 size = (bfd_size_type) bfd_get_reloc_size (howto);
10675 buf = (bfd_byte *) bfd_zmalloc (size);
10676 if (buf == NULL && size != 0)
10678 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10685 case bfd_reloc_outofrange:
10688 case bfd_reloc_overflow:
10689 if (link_order->type == bfd_section_reloc_link_order)
10690 sym_name = bfd_section_name (output_bfd,
10691 link_order->u.reloc.p->u.section);
10693 sym_name = link_order->u.reloc.p->u.name;
10694 if (! ((*info->callbacks->reloc_overflow)
10695 (info, NULL, sym_name, howto->name, addend, NULL,
10696 NULL, (bfd_vma) 0)))
10703 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10704 link_order->offset, size);
10710 /* The address of a reloc is relative to the section in a
10711 relocatable file, and is a virtual address in an executable
10713 offset = link_order->offset;
10714 if (! bfd_link_relocatable (info))
10715 offset += output_section->vma;
10717 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10719 irel[i].r_offset = offset;
10720 irel[i].r_info = 0;
10721 irel[i].r_addend = 0;
10723 if (bed->s->arch_size == 32)
10724 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10726 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10728 rel_hdr = reldata->hdr;
10729 erel = rel_hdr->contents;
10730 if (rel_hdr->sh_type == SHT_REL)
10732 erel += reldata->count * bed->s->sizeof_rel;
10733 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10737 irel[0].r_addend = addend;
10738 erel += reldata->count * bed->s->sizeof_rela;
10739 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10748 /* Get the output vma of the section pointed to by the sh_link field. */
10751 elf_get_linked_section_vma (struct bfd_link_order *p)
10753 Elf_Internal_Shdr **elf_shdrp;
10757 s = p->u.indirect.section;
10758 elf_shdrp = elf_elfsections (s->owner);
10759 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10760 elfsec = elf_shdrp[elfsec]->sh_link;
10762 The Intel C compiler generates SHT_IA_64_UNWIND with
10763 SHF_LINK_ORDER. But it doesn't set the sh_link or
10764 sh_info fields. Hence we could get the situation
10765 where elfsec is 0. */
10768 const struct elf_backend_data *bed
10769 = get_elf_backend_data (s->owner);
10770 if (bed->link_order_error_handler)
10771 bed->link_order_error_handler
10772 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10777 s = elf_shdrp[elfsec]->bfd_section;
10778 return s->output_section->vma + s->output_offset;
10783 /* Compare two sections based on the locations of the sections they are
10784 linked to. Used by elf_fixup_link_order. */
10787 compare_link_order (const void * a, const void * b)
10792 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10793 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10796 return apos > bpos;
10800 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10801 order as their linked sections. Returns false if this could not be done
10802 because an output section includes both ordered and unordered
10803 sections. Ideally we'd do this in the linker proper. */
10806 elf_fixup_link_order (bfd *abfd, asection *o)
10808 int seen_linkorder;
10811 struct bfd_link_order *p;
10813 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10815 struct bfd_link_order **sections;
10816 asection *s, *other_sec, *linkorder_sec;
10820 linkorder_sec = NULL;
10822 seen_linkorder = 0;
10823 for (p = o->map_head.link_order; p != NULL; p = p->next)
10825 if (p->type == bfd_indirect_link_order)
10827 s = p->u.indirect.section;
10829 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10830 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10831 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10832 && elfsec < elf_numsections (sub)
10833 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10834 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10848 if (seen_other && seen_linkorder)
10850 if (other_sec && linkorder_sec)
10851 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10853 linkorder_sec->owner, other_sec,
10856 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10858 bfd_set_error (bfd_error_bad_value);
10863 if (!seen_linkorder)
10866 sections = (struct bfd_link_order **)
10867 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10868 if (sections == NULL)
10870 seen_linkorder = 0;
10872 for (p = o->map_head.link_order; p != NULL; p = p->next)
10874 sections[seen_linkorder++] = p;
10876 /* Sort the input sections in the order of their linked section. */
10877 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10878 compare_link_order);
10880 /* Change the offsets of the sections. */
10882 for (n = 0; n < seen_linkorder; n++)
10884 s = sections[n]->u.indirect.section;
10885 offset &= ~(bfd_vma) 0 << s->alignment_power;
10886 s->output_offset = offset;
10887 sections[n]->offset = offset;
10888 /* FIXME: octets_per_byte. */
10889 offset += sections[n]->size;
10897 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10901 if (flinfo->symstrtab != NULL)
10902 _bfd_elf_strtab_free (flinfo->symstrtab);
10903 if (flinfo->contents != NULL)
10904 free (flinfo->contents);
10905 if (flinfo->external_relocs != NULL)
10906 free (flinfo->external_relocs);
10907 if (flinfo->internal_relocs != NULL)
10908 free (flinfo->internal_relocs);
10909 if (flinfo->external_syms != NULL)
10910 free (flinfo->external_syms);
10911 if (flinfo->locsym_shndx != NULL)
10912 free (flinfo->locsym_shndx);
10913 if (flinfo->internal_syms != NULL)
10914 free (flinfo->internal_syms);
10915 if (flinfo->indices != NULL)
10916 free (flinfo->indices);
10917 if (flinfo->sections != NULL)
10918 free (flinfo->sections);
10919 if (flinfo->symshndxbuf != NULL)
10920 free (flinfo->symshndxbuf);
10921 for (o = obfd->sections; o != NULL; o = o->next)
10923 struct bfd_elf_section_data *esdo = elf_section_data (o);
10924 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10925 free (esdo->rel.hashes);
10926 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10927 free (esdo->rela.hashes);
10931 /* Do the final step of an ELF link. */
10934 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10936 bfd_boolean dynamic;
10937 bfd_boolean emit_relocs;
10939 struct elf_final_link_info flinfo;
10941 struct bfd_link_order *p;
10943 bfd_size_type max_contents_size;
10944 bfd_size_type max_external_reloc_size;
10945 bfd_size_type max_internal_reloc_count;
10946 bfd_size_type max_sym_count;
10947 bfd_size_type max_sym_shndx_count;
10948 Elf_Internal_Sym elfsym;
10950 Elf_Internal_Shdr *symtab_hdr;
10951 Elf_Internal_Shdr *symtab_shndx_hdr;
10952 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10953 struct elf_outext_info eoinfo;
10954 bfd_boolean merged;
10955 size_t relativecount = 0;
10956 asection *reldyn = 0;
10958 asection *attr_section = NULL;
10959 bfd_vma attr_size = 0;
10960 const char *std_attrs_section;
10962 if (! is_elf_hash_table (info->hash))
10965 if (bfd_link_pic (info))
10966 abfd->flags |= DYNAMIC;
10968 dynamic = elf_hash_table (info)->dynamic_sections_created;
10969 dynobj = elf_hash_table (info)->dynobj;
10971 emit_relocs = (bfd_link_relocatable (info)
10972 || info->emitrelocations);
10974 flinfo.info = info;
10975 flinfo.output_bfd = abfd;
10976 flinfo.symstrtab = _bfd_elf_strtab_init ();
10977 if (flinfo.symstrtab == NULL)
10982 flinfo.hash_sec = NULL;
10983 flinfo.symver_sec = NULL;
10987 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10988 /* Note that dynsym_sec can be NULL (on VMS). */
10989 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10990 /* Note that it is OK if symver_sec is NULL. */
10993 flinfo.contents = NULL;
10994 flinfo.external_relocs = NULL;
10995 flinfo.internal_relocs = NULL;
10996 flinfo.external_syms = NULL;
10997 flinfo.locsym_shndx = NULL;
10998 flinfo.internal_syms = NULL;
10999 flinfo.indices = NULL;
11000 flinfo.sections = NULL;
11001 flinfo.symshndxbuf = NULL;
11002 flinfo.filesym_count = 0;
11004 /* The object attributes have been merged. Remove the input
11005 sections from the link, and set the contents of the output
11007 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11008 for (o = abfd->sections; o != NULL; o = o->next)
11010 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11011 || strcmp (o->name, ".gnu.attributes") == 0)
11013 for (p = o->map_head.link_order; p != NULL; p = p->next)
11015 asection *input_section;
11017 if (p->type != bfd_indirect_link_order)
11019 input_section = p->u.indirect.section;
11020 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11021 elf_link_input_bfd ignores this section. */
11022 input_section->flags &= ~SEC_HAS_CONTENTS;
11025 attr_size = bfd_elf_obj_attr_size (abfd);
11028 bfd_set_section_size (abfd, o, attr_size);
11030 /* Skip this section later on. */
11031 o->map_head.link_order = NULL;
11034 o->flags |= SEC_EXCLUDE;
11038 /* Count up the number of relocations we will output for each output
11039 section, so that we know the sizes of the reloc sections. We
11040 also figure out some maximum sizes. */
11041 max_contents_size = 0;
11042 max_external_reloc_size = 0;
11043 max_internal_reloc_count = 0;
11045 max_sym_shndx_count = 0;
11047 for (o = abfd->sections; o != NULL; o = o->next)
11049 struct bfd_elf_section_data *esdo = elf_section_data (o);
11050 o->reloc_count = 0;
11052 for (p = o->map_head.link_order; p != NULL; p = p->next)
11054 unsigned int reloc_count = 0;
11055 unsigned int additional_reloc_count = 0;
11056 struct bfd_elf_section_data *esdi = NULL;
11058 if (p->type == bfd_section_reloc_link_order
11059 || p->type == bfd_symbol_reloc_link_order)
11061 else if (p->type == bfd_indirect_link_order)
11065 sec = p->u.indirect.section;
11066 esdi = elf_section_data (sec);
11068 /* Mark all sections which are to be included in the
11069 link. This will normally be every section. We need
11070 to do this so that we can identify any sections which
11071 the linker has decided to not include. */
11072 sec->linker_mark = TRUE;
11074 if (sec->flags & SEC_MERGE)
11077 if (esdo->this_hdr.sh_type == SHT_REL
11078 || esdo->this_hdr.sh_type == SHT_RELA)
11079 /* Some backends use reloc_count in relocation sections
11080 to count particular types of relocs. Of course,
11081 reloc sections themselves can't have relocations. */
11083 else if (emit_relocs)
11085 reloc_count = sec->reloc_count;
11086 if (bed->elf_backend_count_additional_relocs)
11089 c = (*bed->elf_backend_count_additional_relocs) (sec);
11090 additional_reloc_count += c;
11093 else if (bed->elf_backend_count_relocs)
11094 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11096 if (sec->rawsize > max_contents_size)
11097 max_contents_size = sec->rawsize;
11098 if (sec->size > max_contents_size)
11099 max_contents_size = sec->size;
11101 /* We are interested in just local symbols, not all
11103 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11104 && (sec->owner->flags & DYNAMIC) == 0)
11108 if (elf_bad_symtab (sec->owner))
11109 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11110 / bed->s->sizeof_sym);
11112 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11114 if (sym_count > max_sym_count)
11115 max_sym_count = sym_count;
11117 if (sym_count > max_sym_shndx_count
11118 && elf_symtab_shndx_list (sec->owner) != NULL)
11119 max_sym_shndx_count = sym_count;
11121 if ((sec->flags & SEC_RELOC) != 0)
11123 size_t ext_size = 0;
11125 if (esdi->rel.hdr != NULL)
11126 ext_size = esdi->rel.hdr->sh_size;
11127 if (esdi->rela.hdr != NULL)
11128 ext_size += esdi->rela.hdr->sh_size;
11130 if (ext_size > max_external_reloc_size)
11131 max_external_reloc_size = ext_size;
11132 if (sec->reloc_count > max_internal_reloc_count)
11133 max_internal_reloc_count = sec->reloc_count;
11138 if (reloc_count == 0)
11141 reloc_count += additional_reloc_count;
11142 o->reloc_count += reloc_count;
11144 if (p->type == bfd_indirect_link_order && emit_relocs)
11148 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11149 esdo->rel.count += additional_reloc_count;
11151 if (esdi->rela.hdr)
11153 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11154 esdo->rela.count += additional_reloc_count;
11160 esdo->rela.count += reloc_count;
11162 esdo->rel.count += reloc_count;
11166 if (o->reloc_count > 0)
11167 o->flags |= SEC_RELOC;
11170 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11171 set it (this is probably a bug) and if it is set
11172 assign_section_numbers will create a reloc section. */
11173 o->flags &=~ SEC_RELOC;
11176 /* If the SEC_ALLOC flag is not set, force the section VMA to
11177 zero. This is done in elf_fake_sections as well, but forcing
11178 the VMA to 0 here will ensure that relocs against these
11179 sections are handled correctly. */
11180 if ((o->flags & SEC_ALLOC) == 0
11181 && ! o->user_set_vma)
11185 if (! bfd_link_relocatable (info) && merged)
11186 elf_link_hash_traverse (elf_hash_table (info),
11187 _bfd_elf_link_sec_merge_syms, abfd);
11189 /* Figure out the file positions for everything but the symbol table
11190 and the relocs. We set symcount to force assign_section_numbers
11191 to create a symbol table. */
11192 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11193 BFD_ASSERT (! abfd->output_has_begun);
11194 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11197 /* Set sizes, and assign file positions for reloc sections. */
11198 for (o = abfd->sections; o != NULL; o = o->next)
11200 struct bfd_elf_section_data *esdo = elf_section_data (o);
11201 if ((o->flags & SEC_RELOC) != 0)
11204 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11208 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11212 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11213 to count upwards while actually outputting the relocations. */
11214 esdo->rel.count = 0;
11215 esdo->rela.count = 0;
11217 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11219 /* Cache the section contents so that they can be compressed
11220 later. Use bfd_malloc since it will be freed by
11221 bfd_compress_section_contents. */
11222 unsigned char *contents = esdo->this_hdr.contents;
11223 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11226 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11227 if (contents == NULL)
11229 esdo->this_hdr.contents = contents;
11233 /* We have now assigned file positions for all the sections except
11234 .symtab, .strtab, and non-loaded reloc sections. We start the
11235 .symtab section at the current file position, and write directly
11236 to it. We build the .strtab section in memory. */
11237 bfd_get_symcount (abfd) = 0;
11238 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11239 /* sh_name is set in prep_headers. */
11240 symtab_hdr->sh_type = SHT_SYMTAB;
11241 /* sh_flags, sh_addr and sh_size all start off zero. */
11242 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11243 /* sh_link is set in assign_section_numbers. */
11244 /* sh_info is set below. */
11245 /* sh_offset is set just below. */
11246 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11248 if (max_sym_count < 20)
11249 max_sym_count = 20;
11250 elf_hash_table (info)->strtabsize = max_sym_count;
11251 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11252 elf_hash_table (info)->strtab
11253 = (struct elf_sym_strtab *) bfd_malloc (amt);
11254 if (elf_hash_table (info)->strtab == NULL)
11256 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11258 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11259 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11261 if (info->strip != strip_all || emit_relocs)
11263 file_ptr off = elf_next_file_pos (abfd);
11265 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11267 /* Note that at this point elf_next_file_pos (abfd) is
11268 incorrect. We do not yet know the size of the .symtab section.
11269 We correct next_file_pos below, after we do know the size. */
11271 /* Start writing out the symbol table. The first symbol is always a
11273 elfsym.st_value = 0;
11274 elfsym.st_size = 0;
11275 elfsym.st_info = 0;
11276 elfsym.st_other = 0;
11277 elfsym.st_shndx = SHN_UNDEF;
11278 elfsym.st_target_internal = 0;
11279 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11280 bfd_und_section_ptr, NULL) != 1)
11283 /* Output a symbol for each section. We output these even if we are
11284 discarding local symbols, since they are used for relocs. These
11285 symbols have no names. We store the index of each one in the
11286 index field of the section, so that we can find it again when
11287 outputting relocs. */
11289 elfsym.st_size = 0;
11290 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11291 elfsym.st_other = 0;
11292 elfsym.st_value = 0;
11293 elfsym.st_target_internal = 0;
11294 for (i = 1; i < elf_numsections (abfd); i++)
11296 o = bfd_section_from_elf_index (abfd, i);
11299 o->target_index = bfd_get_symcount (abfd);
11300 elfsym.st_shndx = i;
11301 if (!bfd_link_relocatable (info))
11302 elfsym.st_value = o->vma;
11303 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11310 /* Allocate some memory to hold information read in from the input
11312 if (max_contents_size != 0)
11314 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11315 if (flinfo.contents == NULL)
11319 if (max_external_reloc_size != 0)
11321 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11322 if (flinfo.external_relocs == NULL)
11326 if (max_internal_reloc_count != 0)
11328 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11329 amt *= sizeof (Elf_Internal_Rela);
11330 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11331 if (flinfo.internal_relocs == NULL)
11335 if (max_sym_count != 0)
11337 amt = max_sym_count * bed->s->sizeof_sym;
11338 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11339 if (flinfo.external_syms == NULL)
11342 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11343 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11344 if (flinfo.internal_syms == NULL)
11347 amt = max_sym_count * sizeof (long);
11348 flinfo.indices = (long int *) bfd_malloc (amt);
11349 if (flinfo.indices == NULL)
11352 amt = max_sym_count * sizeof (asection *);
11353 flinfo.sections = (asection **) bfd_malloc (amt);
11354 if (flinfo.sections == NULL)
11358 if (max_sym_shndx_count != 0)
11360 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11361 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11362 if (flinfo.locsym_shndx == NULL)
11366 if (elf_hash_table (info)->tls_sec)
11368 bfd_vma base, end = 0;
11371 for (sec = elf_hash_table (info)->tls_sec;
11372 sec && (sec->flags & SEC_THREAD_LOCAL);
11375 bfd_size_type size = sec->size;
11378 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11380 struct bfd_link_order *ord = sec->map_tail.link_order;
11383 size = ord->offset + ord->size;
11385 end = sec->vma + size;
11387 base = elf_hash_table (info)->tls_sec->vma;
11388 /* Only align end of TLS section if static TLS doesn't have special
11389 alignment requirements. */
11390 if (bed->static_tls_alignment == 1)
11391 end = align_power (end,
11392 elf_hash_table (info)->tls_sec->alignment_power);
11393 elf_hash_table (info)->tls_size = end - base;
11396 /* Reorder SHF_LINK_ORDER sections. */
11397 for (o = abfd->sections; o != NULL; o = o->next)
11399 if (!elf_fixup_link_order (abfd, o))
11403 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11406 /* Since ELF permits relocations to be against local symbols, we
11407 must have the local symbols available when we do the relocations.
11408 Since we would rather only read the local symbols once, and we
11409 would rather not keep them in memory, we handle all the
11410 relocations for a single input file at the same time.
11412 Unfortunately, there is no way to know the total number of local
11413 symbols until we have seen all of them, and the local symbol
11414 indices precede the global symbol indices. This means that when
11415 we are generating relocatable output, and we see a reloc against
11416 a global symbol, we can not know the symbol index until we have
11417 finished examining all the local symbols to see which ones we are
11418 going to output. To deal with this, we keep the relocations in
11419 memory, and don't output them until the end of the link. This is
11420 an unfortunate waste of memory, but I don't see a good way around
11421 it. Fortunately, it only happens when performing a relocatable
11422 link, which is not the common case. FIXME: If keep_memory is set
11423 we could write the relocs out and then read them again; I don't
11424 know how bad the memory loss will be. */
11426 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11427 sub->output_has_begun = FALSE;
11428 for (o = abfd->sections; o != NULL; o = o->next)
11430 for (p = o->map_head.link_order; p != NULL; p = p->next)
11432 if (p->type == bfd_indirect_link_order
11433 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11434 == bfd_target_elf_flavour)
11435 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11437 if (! sub->output_has_begun)
11439 if (! elf_link_input_bfd (&flinfo, sub))
11441 sub->output_has_begun = TRUE;
11444 else if (p->type == bfd_section_reloc_link_order
11445 || p->type == bfd_symbol_reloc_link_order)
11447 if (! elf_reloc_link_order (abfd, info, o, p))
11452 if (! _bfd_default_link_order (abfd, info, o, p))
11454 if (p->type == bfd_indirect_link_order
11455 && (bfd_get_flavour (sub)
11456 == bfd_target_elf_flavour)
11457 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11458 != bed->s->elfclass))
11460 const char *iclass, *oclass;
11462 switch (bed->s->elfclass)
11464 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11465 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11466 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11470 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11472 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11473 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11474 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11478 bfd_set_error (bfd_error_wrong_format);
11479 (*_bfd_error_handler)
11480 (_("%B: file class %s incompatible with %s"),
11481 sub, iclass, oclass);
11490 /* Free symbol buffer if needed. */
11491 if (!info->reduce_memory_overheads)
11493 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11494 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11495 && elf_tdata (sub)->symbuf)
11497 free (elf_tdata (sub)->symbuf);
11498 elf_tdata (sub)->symbuf = NULL;
11502 /* Output any global symbols that got converted to local in a
11503 version script or due to symbol visibility. We do this in a
11504 separate step since ELF requires all local symbols to appear
11505 prior to any global symbols. FIXME: We should only do this if
11506 some global symbols were, in fact, converted to become local.
11507 FIXME: Will this work correctly with the Irix 5 linker? */
11508 eoinfo.failed = FALSE;
11509 eoinfo.flinfo = &flinfo;
11510 eoinfo.localsyms = TRUE;
11511 eoinfo.file_sym_done = FALSE;
11512 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11516 /* If backend needs to output some local symbols not present in the hash
11517 table, do it now. */
11518 if (bed->elf_backend_output_arch_local_syms
11519 && (info->strip != strip_all || emit_relocs))
11521 typedef int (*out_sym_func)
11522 (void *, const char *, Elf_Internal_Sym *, asection *,
11523 struct elf_link_hash_entry *);
11525 if (! ((*bed->elf_backend_output_arch_local_syms)
11526 (abfd, info, &flinfo,
11527 (out_sym_func) elf_link_output_symstrtab)))
11531 /* That wrote out all the local symbols. Finish up the symbol table
11532 with the global symbols. Even if we want to strip everything we
11533 can, we still need to deal with those global symbols that got
11534 converted to local in a version script. */
11536 /* The sh_info field records the index of the first non local symbol. */
11537 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11540 && elf_hash_table (info)->dynsym != NULL
11541 && (elf_hash_table (info)->dynsym->output_section
11542 != bfd_abs_section_ptr))
11544 Elf_Internal_Sym sym;
11545 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11546 long last_local = 0;
11548 /* Write out the section symbols for the output sections. */
11549 if (bfd_link_pic (info)
11550 || elf_hash_table (info)->is_relocatable_executable)
11556 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11558 sym.st_target_internal = 0;
11560 for (s = abfd->sections; s != NULL; s = s->next)
11566 dynindx = elf_section_data (s)->dynindx;
11569 indx = elf_section_data (s)->this_idx;
11570 BFD_ASSERT (indx > 0);
11571 sym.st_shndx = indx;
11572 if (! check_dynsym (abfd, &sym))
11574 sym.st_value = s->vma;
11575 dest = dynsym + dynindx * bed->s->sizeof_sym;
11576 if (last_local < dynindx)
11577 last_local = dynindx;
11578 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11582 /* Write out the local dynsyms. */
11583 if (elf_hash_table (info)->dynlocal)
11585 struct elf_link_local_dynamic_entry *e;
11586 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11591 /* Copy the internal symbol and turn off visibility.
11592 Note that we saved a word of storage and overwrote
11593 the original st_name with the dynstr_index. */
11595 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11597 s = bfd_section_from_elf_index (e->input_bfd,
11602 elf_section_data (s->output_section)->this_idx;
11603 if (! check_dynsym (abfd, &sym))
11605 sym.st_value = (s->output_section->vma
11607 + e->isym.st_value);
11610 if (last_local < e->dynindx)
11611 last_local = e->dynindx;
11613 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11614 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11618 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11622 /* We get the global symbols from the hash table. */
11623 eoinfo.failed = FALSE;
11624 eoinfo.localsyms = FALSE;
11625 eoinfo.flinfo = &flinfo;
11626 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11630 /* If backend needs to output some symbols not present in the hash
11631 table, do it now. */
11632 if (bed->elf_backend_output_arch_syms
11633 && (info->strip != strip_all || emit_relocs))
11635 typedef int (*out_sym_func)
11636 (void *, const char *, Elf_Internal_Sym *, asection *,
11637 struct elf_link_hash_entry *);
11639 if (! ((*bed->elf_backend_output_arch_syms)
11640 (abfd, info, &flinfo,
11641 (out_sym_func) elf_link_output_symstrtab)))
11645 /* Finalize the .strtab section. */
11646 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11648 /* Swap out the .strtab section. */
11649 if (!elf_link_swap_symbols_out (&flinfo))
11652 /* Now we know the size of the symtab section. */
11653 if (bfd_get_symcount (abfd) > 0)
11655 /* Finish up and write out the symbol string table (.strtab)
11657 Elf_Internal_Shdr *symstrtab_hdr;
11658 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11660 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11661 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11663 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11664 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11665 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11666 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11667 symtab_shndx_hdr->sh_size = amt;
11669 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11672 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11673 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11677 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11678 /* sh_name was set in prep_headers. */
11679 symstrtab_hdr->sh_type = SHT_STRTAB;
11680 symstrtab_hdr->sh_flags = 0;
11681 symstrtab_hdr->sh_addr = 0;
11682 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11683 symstrtab_hdr->sh_entsize = 0;
11684 symstrtab_hdr->sh_link = 0;
11685 symstrtab_hdr->sh_info = 0;
11686 /* sh_offset is set just below. */
11687 symstrtab_hdr->sh_addralign = 1;
11689 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11691 elf_next_file_pos (abfd) = off;
11693 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11694 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11698 /* Adjust the relocs to have the correct symbol indices. */
11699 for (o = abfd->sections; o != NULL; o = o->next)
11701 struct bfd_elf_section_data *esdo = elf_section_data (o);
11703 if ((o->flags & SEC_RELOC) == 0)
11706 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11707 if (esdo->rel.hdr != NULL
11708 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11710 if (esdo->rela.hdr != NULL
11711 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11714 /* Set the reloc_count field to 0 to prevent write_relocs from
11715 trying to swap the relocs out itself. */
11716 o->reloc_count = 0;
11719 if (dynamic && info->combreloc && dynobj != NULL)
11720 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11722 /* If we are linking against a dynamic object, or generating a
11723 shared library, finish up the dynamic linking information. */
11726 bfd_byte *dyncon, *dynconend;
11728 /* Fix up .dynamic entries. */
11729 o = bfd_get_linker_section (dynobj, ".dynamic");
11730 BFD_ASSERT (o != NULL);
11732 dyncon = o->contents;
11733 dynconend = o->contents + o->size;
11734 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11736 Elf_Internal_Dyn dyn;
11740 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11747 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11749 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11751 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11752 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11755 dyn.d_un.d_val = relativecount;
11762 name = info->init_function;
11765 name = info->fini_function;
11768 struct elf_link_hash_entry *h;
11770 h = elf_link_hash_lookup (elf_hash_table (info), name,
11771 FALSE, FALSE, TRUE);
11773 && (h->root.type == bfd_link_hash_defined
11774 || h->root.type == bfd_link_hash_defweak))
11776 dyn.d_un.d_ptr = h->root.u.def.value;
11777 o = h->root.u.def.section;
11778 if (o->output_section != NULL)
11779 dyn.d_un.d_ptr += (o->output_section->vma
11780 + o->output_offset);
11783 /* The symbol is imported from another shared
11784 library and does not apply to this one. */
11785 dyn.d_un.d_ptr = 0;
11792 case DT_PREINIT_ARRAYSZ:
11793 name = ".preinit_array";
11795 case DT_INIT_ARRAYSZ:
11796 name = ".init_array";
11798 case DT_FINI_ARRAYSZ:
11799 name = ".fini_array";
11801 o = bfd_get_section_by_name (abfd, name);
11804 (*_bfd_error_handler)
11805 (_("%B: could not find output section %s"), abfd, name);
11809 (*_bfd_error_handler)
11810 (_("warning: %s section has zero size"), name);
11811 dyn.d_un.d_val = o->size;
11814 case DT_PREINIT_ARRAY:
11815 name = ".preinit_array";
11817 case DT_INIT_ARRAY:
11818 name = ".init_array";
11820 case DT_FINI_ARRAY:
11821 name = ".fini_array";
11828 name = ".gnu.hash";
11837 name = ".gnu.version_d";
11840 name = ".gnu.version_r";
11843 name = ".gnu.version";
11845 o = bfd_get_section_by_name (abfd, name);
11848 (*_bfd_error_handler)
11849 (_("%B: could not find output section %s"), abfd, name);
11852 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11854 (*_bfd_error_handler)
11855 (_("warning: section '%s' is being made into a note"), name);
11856 bfd_set_error (bfd_error_nonrepresentable_section);
11859 dyn.d_un.d_ptr = o->vma;
11866 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11870 dyn.d_un.d_val = 0;
11871 dyn.d_un.d_ptr = 0;
11872 for (i = 1; i < elf_numsections (abfd); i++)
11874 Elf_Internal_Shdr *hdr;
11876 hdr = elf_elfsections (abfd)[i];
11877 if (hdr->sh_type == type
11878 && (hdr->sh_flags & SHF_ALLOC) != 0)
11880 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11881 dyn.d_un.d_val += hdr->sh_size;
11884 if (dyn.d_un.d_ptr == 0
11885 || hdr->sh_addr < dyn.d_un.d_ptr)
11886 dyn.d_un.d_ptr = hdr->sh_addr;
11892 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11896 /* If we have created any dynamic sections, then output them. */
11897 if (dynobj != NULL)
11899 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11902 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11903 if (((info->warn_shared_textrel && bfd_link_pic (info))
11904 || info->error_textrel)
11905 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11907 bfd_byte *dyncon, *dynconend;
11909 dyncon = o->contents;
11910 dynconend = o->contents + o->size;
11911 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11913 Elf_Internal_Dyn dyn;
11915 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11917 if (dyn.d_tag == DT_TEXTREL)
11919 if (info->error_textrel)
11920 info->callbacks->einfo
11921 (_("%P%X: read-only segment has dynamic relocations.\n"));
11923 info->callbacks->einfo
11924 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11930 for (o = dynobj->sections; o != NULL; o = o->next)
11932 if ((o->flags & SEC_HAS_CONTENTS) == 0
11934 || o->output_section == bfd_abs_section_ptr)
11936 if ((o->flags & SEC_LINKER_CREATED) == 0)
11938 /* At this point, we are only interested in sections
11939 created by _bfd_elf_link_create_dynamic_sections. */
11942 if (elf_hash_table (info)->stab_info.stabstr == o)
11944 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11946 if (strcmp (o->name, ".dynstr") != 0)
11948 /* FIXME: octets_per_byte. */
11949 if (! bfd_set_section_contents (abfd, o->output_section,
11951 (file_ptr) o->output_offset,
11957 /* The contents of the .dynstr section are actually in a
11961 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11962 if (bfd_seek (abfd, off, SEEK_SET) != 0
11963 || ! _bfd_elf_strtab_emit (abfd,
11964 elf_hash_table (info)->dynstr))
11970 if (bfd_link_relocatable (info))
11972 bfd_boolean failed = FALSE;
11974 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11979 /* If we have optimized stabs strings, output them. */
11980 if (elf_hash_table (info)->stab_info.stabstr != NULL)
11982 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11986 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11989 elf_final_link_free (abfd, &flinfo);
11991 elf_linker (abfd) = TRUE;
11995 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11996 if (contents == NULL)
11997 return FALSE; /* Bail out and fail. */
11998 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11999 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12006 elf_final_link_free (abfd, &flinfo);
12010 /* Initialize COOKIE for input bfd ABFD. */
12013 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12014 struct bfd_link_info *info, bfd *abfd)
12016 Elf_Internal_Shdr *symtab_hdr;
12017 const struct elf_backend_data *bed;
12019 bed = get_elf_backend_data (abfd);
12020 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12022 cookie->abfd = abfd;
12023 cookie->sym_hashes = elf_sym_hashes (abfd);
12024 cookie->bad_symtab = elf_bad_symtab (abfd);
12025 if (cookie->bad_symtab)
12027 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12028 cookie->extsymoff = 0;
12032 cookie->locsymcount = symtab_hdr->sh_info;
12033 cookie->extsymoff = symtab_hdr->sh_info;
12036 if (bed->s->arch_size == 32)
12037 cookie->r_sym_shift = 8;
12039 cookie->r_sym_shift = 32;
12041 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12042 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12044 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12045 cookie->locsymcount, 0,
12047 if (cookie->locsyms == NULL)
12049 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12052 if (info->keep_memory)
12053 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12058 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12061 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12063 Elf_Internal_Shdr *symtab_hdr;
12065 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12066 if (cookie->locsyms != NULL
12067 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12068 free (cookie->locsyms);
12071 /* Initialize the relocation information in COOKIE for input section SEC
12072 of input bfd ABFD. */
12075 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12076 struct bfd_link_info *info, bfd *abfd,
12079 const struct elf_backend_data *bed;
12081 if (sec->reloc_count == 0)
12083 cookie->rels = NULL;
12084 cookie->relend = NULL;
12088 bed = get_elf_backend_data (abfd);
12090 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12091 info->keep_memory);
12092 if (cookie->rels == NULL)
12094 cookie->rel = cookie->rels;
12095 cookie->relend = (cookie->rels
12096 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12098 cookie->rel = cookie->rels;
12102 /* Free the memory allocated by init_reloc_cookie_rels,
12106 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12109 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12110 free (cookie->rels);
12113 /* Initialize the whole of COOKIE for input section SEC. */
12116 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12117 struct bfd_link_info *info,
12120 if (!init_reloc_cookie (cookie, info, sec->owner))
12122 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12127 fini_reloc_cookie (cookie, sec->owner);
12132 /* Free the memory allocated by init_reloc_cookie_for_section,
12136 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12139 fini_reloc_cookie_rels (cookie, sec);
12140 fini_reloc_cookie (cookie, sec->owner);
12143 /* Garbage collect unused sections. */
12145 /* Default gc_mark_hook. */
12148 _bfd_elf_gc_mark_hook (asection *sec,
12149 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12150 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12151 struct elf_link_hash_entry *h,
12152 Elf_Internal_Sym *sym)
12156 switch (h->root.type)
12158 case bfd_link_hash_defined:
12159 case bfd_link_hash_defweak:
12160 return h->root.u.def.section;
12162 case bfd_link_hash_common:
12163 return h->root.u.c.p->section;
12170 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12175 /* COOKIE->rel describes a relocation against section SEC, which is
12176 a section we've decided to keep. Return the section that contains
12177 the relocation symbol, or NULL if no section contains it. */
12180 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12181 elf_gc_mark_hook_fn gc_mark_hook,
12182 struct elf_reloc_cookie *cookie,
12183 bfd_boolean *start_stop)
12185 unsigned long r_symndx;
12186 struct elf_link_hash_entry *h;
12188 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12189 if (r_symndx == STN_UNDEF)
12192 if (r_symndx >= cookie->locsymcount
12193 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12195 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12198 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12202 while (h->root.type == bfd_link_hash_indirect
12203 || h->root.type == bfd_link_hash_warning)
12204 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12206 /* If this symbol is weak and there is a non-weak definition, we
12207 keep the non-weak definition because many backends put
12208 dynamic reloc info on the non-weak definition for code
12209 handling copy relocs. */
12210 if (h->u.weakdef != NULL)
12211 h->u.weakdef->mark = 1;
12213 if (start_stop != NULL
12214 && (h->root.type == bfd_link_hash_undefined
12215 || h->root.type == bfd_link_hash_undefweak))
12217 /* To work around a glibc bug, mark all XXX input sections
12218 when there is an as yet undefined reference to __start_XXX
12219 or __stop_XXX symbols. The linker will later define such
12220 symbols for orphan input sections that have a name
12221 representable as a C identifier. */
12222 const char *sec_name = NULL;
12223 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12224 sec_name = h->root.root.string + 8;
12225 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12226 sec_name = h->root.root.string + 7;
12228 if (sec_name != NULL && *sec_name != '\0')
12232 for (i = info->input_bfds; i != NULL; i = i->link.next)
12234 asection *s = bfd_get_section_by_name (i, sec_name);
12235 if (s != NULL && !s->gc_mark)
12237 *start_stop = TRUE;
12244 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12247 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12248 &cookie->locsyms[r_symndx]);
12251 /* COOKIE->rel describes a relocation against section SEC, which is
12252 a section we've decided to keep. Mark the section that contains
12253 the relocation symbol. */
12256 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12258 elf_gc_mark_hook_fn gc_mark_hook,
12259 struct elf_reloc_cookie *cookie)
12262 bfd_boolean start_stop = FALSE;
12264 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12265 while (rsec != NULL)
12267 if (!rsec->gc_mark)
12269 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12270 || (rsec->owner->flags & DYNAMIC) != 0)
12272 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12277 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12282 /* The mark phase of garbage collection. For a given section, mark
12283 it and any sections in this section's group, and all the sections
12284 which define symbols to which it refers. */
12287 _bfd_elf_gc_mark (struct bfd_link_info *info,
12289 elf_gc_mark_hook_fn gc_mark_hook)
12292 asection *group_sec, *eh_frame;
12296 /* Mark all the sections in the group. */
12297 group_sec = elf_section_data (sec)->next_in_group;
12298 if (group_sec && !group_sec->gc_mark)
12299 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12302 /* Look through the section relocs. */
12304 eh_frame = elf_eh_frame_section (sec->owner);
12305 if ((sec->flags & SEC_RELOC) != 0
12306 && sec->reloc_count > 0
12307 && sec != eh_frame)
12309 struct elf_reloc_cookie cookie;
12311 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12315 for (; cookie.rel < cookie.relend; cookie.rel++)
12316 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12321 fini_reloc_cookie_for_section (&cookie, sec);
12325 if (ret && eh_frame && elf_fde_list (sec))
12327 struct elf_reloc_cookie cookie;
12329 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12333 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12334 gc_mark_hook, &cookie))
12336 fini_reloc_cookie_for_section (&cookie, eh_frame);
12340 eh_frame = elf_section_eh_frame_entry (sec);
12341 if (ret && eh_frame && !eh_frame->gc_mark)
12342 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12348 /* Scan and mark sections in a special or debug section group. */
12351 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12353 /* Point to first section of section group. */
12355 /* Used to iterate the section group. */
12358 bfd_boolean is_special_grp = TRUE;
12359 bfd_boolean is_debug_grp = TRUE;
12361 /* First scan to see if group contains any section other than debug
12362 and special section. */
12363 ssec = msec = elf_next_in_group (grp);
12366 if ((msec->flags & SEC_DEBUGGING) == 0)
12367 is_debug_grp = FALSE;
12369 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12370 is_special_grp = FALSE;
12372 msec = elf_next_in_group (msec);
12374 while (msec != ssec);
12376 /* If this is a pure debug section group or pure special section group,
12377 keep all sections in this group. */
12378 if (is_debug_grp || is_special_grp)
12383 msec = elf_next_in_group (msec);
12385 while (msec != ssec);
12389 /* Keep debug and special sections. */
12392 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12393 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12397 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12400 bfd_boolean some_kept;
12401 bfd_boolean debug_frag_seen;
12403 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12406 /* Ensure all linker created sections are kept,
12407 see if any other section is already marked,
12408 and note if we have any fragmented debug sections. */
12409 debug_frag_seen = some_kept = FALSE;
12410 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12412 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12414 else if (isec->gc_mark)
12417 if (debug_frag_seen == FALSE
12418 && (isec->flags & SEC_DEBUGGING)
12419 && CONST_STRNEQ (isec->name, ".debug_line."))
12420 debug_frag_seen = TRUE;
12423 /* If no section in this file will be kept, then we can
12424 toss out the debug and special sections. */
12428 /* Keep debug and special sections like .comment when they are
12429 not part of a group. Also keep section groups that contain
12430 just debug sections or special sections. */
12431 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12433 if ((isec->flags & SEC_GROUP) != 0)
12434 _bfd_elf_gc_mark_debug_special_section_group (isec);
12435 else if (((isec->flags & SEC_DEBUGGING) != 0
12436 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12437 && elf_next_in_group (isec) == NULL)
12441 if (! debug_frag_seen)
12444 /* Look for CODE sections which are going to be discarded,
12445 and find and discard any fragmented debug sections which
12446 are associated with that code section. */
12447 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12448 if ((isec->flags & SEC_CODE) != 0
12449 && isec->gc_mark == 0)
12454 ilen = strlen (isec->name);
12456 /* Association is determined by the name of the debug section
12457 containing the name of the code section as a suffix. For
12458 example .debug_line.text.foo is a debug section associated
12460 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12464 if (dsec->gc_mark == 0
12465 || (dsec->flags & SEC_DEBUGGING) == 0)
12468 dlen = strlen (dsec->name);
12471 && strncmp (dsec->name + (dlen - ilen),
12472 isec->name, ilen) == 0)
12482 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12484 struct elf_gc_sweep_symbol_info
12486 struct bfd_link_info *info;
12487 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12492 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12495 && (((h->root.type == bfd_link_hash_defined
12496 || h->root.type == bfd_link_hash_defweak)
12497 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12498 && h->root.u.def.section->gc_mark))
12499 || h->root.type == bfd_link_hash_undefined
12500 || h->root.type == bfd_link_hash_undefweak))
12502 struct elf_gc_sweep_symbol_info *inf;
12504 inf = (struct elf_gc_sweep_symbol_info *) data;
12505 (*inf->hide_symbol) (inf->info, h, TRUE);
12506 h->def_regular = 0;
12507 h->ref_regular = 0;
12508 h->ref_regular_nonweak = 0;
12514 /* The sweep phase of garbage collection. Remove all garbage sections. */
12516 typedef bfd_boolean (*gc_sweep_hook_fn)
12517 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12520 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12523 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12524 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12525 unsigned long section_sym_count;
12526 struct elf_gc_sweep_symbol_info sweep_info;
12528 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12532 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12533 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12536 for (o = sub->sections; o != NULL; o = o->next)
12538 /* When any section in a section group is kept, we keep all
12539 sections in the section group. If the first member of
12540 the section group is excluded, we will also exclude the
12542 if (o->flags & SEC_GROUP)
12544 asection *first = elf_next_in_group (o);
12545 o->gc_mark = first->gc_mark;
12551 /* Skip sweeping sections already excluded. */
12552 if (o->flags & SEC_EXCLUDE)
12555 /* Since this is early in the link process, it is simple
12556 to remove a section from the output. */
12557 o->flags |= SEC_EXCLUDE;
12559 if (info->print_gc_sections && o->size != 0)
12560 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12562 /* But we also have to update some of the relocation
12563 info we collected before. */
12565 && (o->flags & SEC_RELOC) != 0
12566 && o->reloc_count != 0
12567 && !((info->strip == strip_all || info->strip == strip_debugger)
12568 && (o->flags & SEC_DEBUGGING) != 0)
12569 && !bfd_is_abs_section (o->output_section))
12571 Elf_Internal_Rela *internal_relocs;
12575 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12576 info->keep_memory);
12577 if (internal_relocs == NULL)
12580 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12582 if (elf_section_data (o)->relocs != internal_relocs)
12583 free (internal_relocs);
12591 /* Remove the symbols that were in the swept sections from the dynamic
12592 symbol table. GCFIXME: Anyone know how to get them out of the
12593 static symbol table as well? */
12594 sweep_info.info = info;
12595 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12596 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12599 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
12603 /* Propagate collected vtable information. This is called through
12604 elf_link_hash_traverse. */
12607 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12609 /* Those that are not vtables. */
12610 if (h->vtable == NULL || h->vtable->parent == NULL)
12613 /* Those vtables that do not have parents, we cannot merge. */
12614 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12617 /* If we've already been done, exit. */
12618 if (h->vtable->used && h->vtable->used[-1])
12621 /* Make sure the parent's table is up to date. */
12622 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12624 if (h->vtable->used == NULL)
12626 /* None of this table's entries were referenced. Re-use the
12628 h->vtable->used = h->vtable->parent->vtable->used;
12629 h->vtable->size = h->vtable->parent->vtable->size;
12634 bfd_boolean *cu, *pu;
12636 /* Or the parent's entries into ours. */
12637 cu = h->vtable->used;
12639 pu = h->vtable->parent->vtable->used;
12642 const struct elf_backend_data *bed;
12643 unsigned int log_file_align;
12645 bed = get_elf_backend_data (h->root.u.def.section->owner);
12646 log_file_align = bed->s->log_file_align;
12647 n = h->vtable->parent->vtable->size >> log_file_align;
12662 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12665 bfd_vma hstart, hend;
12666 Elf_Internal_Rela *relstart, *relend, *rel;
12667 const struct elf_backend_data *bed;
12668 unsigned int log_file_align;
12670 /* Take care of both those symbols that do not describe vtables as
12671 well as those that are not loaded. */
12672 if (h->vtable == NULL || h->vtable->parent == NULL)
12675 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12676 || h->root.type == bfd_link_hash_defweak);
12678 sec = h->root.u.def.section;
12679 hstart = h->root.u.def.value;
12680 hend = hstart + h->size;
12682 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12684 return *(bfd_boolean *) okp = FALSE;
12685 bed = get_elf_backend_data (sec->owner);
12686 log_file_align = bed->s->log_file_align;
12688 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12690 for (rel = relstart; rel < relend; ++rel)
12691 if (rel->r_offset >= hstart && rel->r_offset < hend)
12693 /* If the entry is in use, do nothing. */
12694 if (h->vtable->used
12695 && (rel->r_offset - hstart) < h->vtable->size)
12697 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12698 if (h->vtable->used[entry])
12701 /* Otherwise, kill it. */
12702 rel->r_offset = rel->r_info = rel->r_addend = 0;
12708 /* Mark sections containing dynamically referenced symbols. When
12709 building shared libraries, we must assume that any visible symbol is
12713 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12715 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12716 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12718 if ((h->root.type == bfd_link_hash_defined
12719 || h->root.type == bfd_link_hash_defweak)
12721 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12722 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12723 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12724 && (!bfd_link_executable (info)
12725 || info->export_dynamic
12728 && (*d->match) (&d->head, NULL, h->root.root.string)))
12729 && (h->versioned >= versioned
12730 || !bfd_hide_sym_by_version (info->version_info,
12731 h->root.root.string)))))
12732 h->root.u.def.section->flags |= SEC_KEEP;
12737 /* Keep all sections containing symbols undefined on the command-line,
12738 and the section containing the entry symbol. */
12741 _bfd_elf_gc_keep (struct bfd_link_info *info)
12743 struct bfd_sym_chain *sym;
12745 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12747 struct elf_link_hash_entry *h;
12749 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12750 FALSE, FALSE, FALSE);
12753 && (h->root.type == bfd_link_hash_defined
12754 || h->root.type == bfd_link_hash_defweak)
12755 && !bfd_is_abs_section (h->root.u.def.section))
12756 h->root.u.def.section->flags |= SEC_KEEP;
12761 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12762 struct bfd_link_info *info)
12764 bfd *ibfd = info->input_bfds;
12766 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12769 struct elf_reloc_cookie cookie;
12771 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12774 if (!init_reloc_cookie (&cookie, info, ibfd))
12777 for (sec = ibfd->sections; sec; sec = sec->next)
12779 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12780 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12782 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12783 fini_reloc_cookie_rels (&cookie, sec);
12790 /* Do mark and sweep of unused sections. */
12793 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12795 bfd_boolean ok = TRUE;
12797 elf_gc_mark_hook_fn gc_mark_hook;
12798 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12799 struct elf_link_hash_table *htab;
12801 if (!bed->can_gc_sections
12802 || !is_elf_hash_table (info->hash))
12804 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12808 bed->gc_keep (info);
12809 htab = elf_hash_table (info);
12811 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12812 at the .eh_frame section if we can mark the FDEs individually. */
12813 for (sub = info->input_bfds;
12814 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12815 sub = sub->link.next)
12818 struct elf_reloc_cookie cookie;
12820 sec = bfd_get_section_by_name (sub, ".eh_frame");
12821 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12823 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12824 if (elf_section_data (sec)->sec_info
12825 && (sec->flags & SEC_LINKER_CREATED) == 0)
12826 elf_eh_frame_section (sub) = sec;
12827 fini_reloc_cookie_for_section (&cookie, sec);
12828 sec = bfd_get_next_section_by_name (NULL, sec);
12832 /* Apply transitive closure to the vtable entry usage info. */
12833 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12837 /* Kill the vtable relocations that were not used. */
12838 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12842 /* Mark dynamically referenced symbols. */
12843 if (htab->dynamic_sections_created)
12844 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12846 /* Grovel through relocs to find out who stays ... */
12847 gc_mark_hook = bed->gc_mark_hook;
12848 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12852 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12853 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12856 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12857 Also treat note sections as a root, if the section is not part
12859 for (o = sub->sections; o != NULL; o = o->next)
12861 && (o->flags & SEC_EXCLUDE) == 0
12862 && ((o->flags & SEC_KEEP) != 0
12863 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12864 && elf_next_in_group (o) == NULL )))
12866 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12871 /* Allow the backend to mark additional target specific sections. */
12872 bed->gc_mark_extra_sections (info, gc_mark_hook);
12874 /* ... and mark SEC_EXCLUDE for those that go. */
12875 return elf_gc_sweep (abfd, info);
12878 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12881 bfd_elf_gc_record_vtinherit (bfd *abfd,
12883 struct elf_link_hash_entry *h,
12886 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12887 struct elf_link_hash_entry **search, *child;
12888 bfd_size_type extsymcount;
12889 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12891 /* The sh_info field of the symtab header tells us where the
12892 external symbols start. We don't care about the local symbols at
12894 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12895 if (!elf_bad_symtab (abfd))
12896 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12898 sym_hashes = elf_sym_hashes (abfd);
12899 sym_hashes_end = sym_hashes + extsymcount;
12901 /* Hunt down the child symbol, which is in this section at the same
12902 offset as the relocation. */
12903 for (search = sym_hashes; search != sym_hashes_end; ++search)
12905 if ((child = *search) != NULL
12906 && (child->root.type == bfd_link_hash_defined
12907 || child->root.type == bfd_link_hash_defweak)
12908 && child->root.u.def.section == sec
12909 && child->root.u.def.value == offset)
12913 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12914 abfd, sec, (unsigned long) offset);
12915 bfd_set_error (bfd_error_invalid_operation);
12919 if (!child->vtable)
12921 child->vtable = ((struct elf_link_virtual_table_entry *)
12922 bfd_zalloc (abfd, sizeof (*child->vtable)));
12923 if (!child->vtable)
12928 /* This *should* only be the absolute section. It could potentially
12929 be that someone has defined a non-global vtable though, which
12930 would be bad. It isn't worth paging in the local symbols to be
12931 sure though; that case should simply be handled by the assembler. */
12933 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12936 child->vtable->parent = h;
12941 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12944 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12945 asection *sec ATTRIBUTE_UNUSED,
12946 struct elf_link_hash_entry *h,
12949 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12950 unsigned int log_file_align = bed->s->log_file_align;
12954 h->vtable = ((struct elf_link_virtual_table_entry *)
12955 bfd_zalloc (abfd, sizeof (*h->vtable)));
12960 if (addend >= h->vtable->size)
12962 size_t size, bytes, file_align;
12963 bfd_boolean *ptr = h->vtable->used;
12965 /* While the symbol is undefined, we have to be prepared to handle
12967 file_align = 1 << log_file_align;
12968 if (h->root.type == bfd_link_hash_undefined)
12969 size = addend + file_align;
12973 if (addend >= size)
12975 /* Oops! We've got a reference past the defined end of
12976 the table. This is probably a bug -- shall we warn? */
12977 size = addend + file_align;
12980 size = (size + file_align - 1) & -file_align;
12982 /* Allocate one extra entry for use as a "done" flag for the
12983 consolidation pass. */
12984 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12988 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12994 oldbytes = (((h->vtable->size >> log_file_align) + 1)
12995 * sizeof (bfd_boolean));
12996 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13000 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13005 /* And arrange for that done flag to be at index -1. */
13006 h->vtable->used = ptr + 1;
13007 h->vtable->size = size;
13010 h->vtable->used[addend >> log_file_align] = TRUE;
13015 /* Map an ELF section header flag to its corresponding string. */
13019 flagword flag_value;
13020 } elf_flags_to_name_table;
13022 static elf_flags_to_name_table elf_flags_to_names [] =
13024 { "SHF_WRITE", SHF_WRITE },
13025 { "SHF_ALLOC", SHF_ALLOC },
13026 { "SHF_EXECINSTR", SHF_EXECINSTR },
13027 { "SHF_MERGE", SHF_MERGE },
13028 { "SHF_STRINGS", SHF_STRINGS },
13029 { "SHF_INFO_LINK", SHF_INFO_LINK},
13030 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13031 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13032 { "SHF_GROUP", SHF_GROUP },
13033 { "SHF_TLS", SHF_TLS },
13034 { "SHF_MASKOS", SHF_MASKOS },
13035 { "SHF_EXCLUDE", SHF_EXCLUDE },
13038 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13040 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13041 struct flag_info *flaginfo,
13044 const bfd_vma sh_flags = elf_section_flags (section);
13046 if (!flaginfo->flags_initialized)
13048 bfd *obfd = info->output_bfd;
13049 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13050 struct flag_info_list *tf = flaginfo->flag_list;
13052 int without_hex = 0;
13054 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13057 flagword (*lookup) (char *);
13059 lookup = bed->elf_backend_lookup_section_flags_hook;
13060 if (lookup != NULL)
13062 flagword hexval = (*lookup) ((char *) tf->name);
13066 if (tf->with == with_flags)
13067 with_hex |= hexval;
13068 else if (tf->with == without_flags)
13069 without_hex |= hexval;
13074 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13076 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13078 if (tf->with == with_flags)
13079 with_hex |= elf_flags_to_names[i].flag_value;
13080 else if (tf->with == without_flags)
13081 without_hex |= elf_flags_to_names[i].flag_value;
13088 info->callbacks->einfo
13089 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13093 flaginfo->flags_initialized = TRUE;
13094 flaginfo->only_with_flags |= with_hex;
13095 flaginfo->not_with_flags |= without_hex;
13098 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13101 if ((flaginfo->not_with_flags & sh_flags) != 0)
13107 struct alloc_got_off_arg {
13109 struct bfd_link_info *info;
13112 /* We need a special top-level link routine to convert got reference counts
13113 to real got offsets. */
13116 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13118 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13119 bfd *obfd = gofarg->info->output_bfd;
13120 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13122 if (h->got.refcount > 0)
13124 h->got.offset = gofarg->gotoff;
13125 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13128 h->got.offset = (bfd_vma) -1;
13133 /* And an accompanying bit to work out final got entry offsets once
13134 we're done. Should be called from final_link. */
13137 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13138 struct bfd_link_info *info)
13141 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13143 struct alloc_got_off_arg gofarg;
13145 BFD_ASSERT (abfd == info->output_bfd);
13147 if (! is_elf_hash_table (info->hash))
13150 /* The GOT offset is relative to the .got section, but the GOT header is
13151 put into the .got.plt section, if the backend uses it. */
13152 if (bed->want_got_plt)
13155 gotoff = bed->got_header_size;
13157 /* Do the local .got entries first. */
13158 for (i = info->input_bfds; i; i = i->link.next)
13160 bfd_signed_vma *local_got;
13161 bfd_size_type j, locsymcount;
13162 Elf_Internal_Shdr *symtab_hdr;
13164 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13167 local_got = elf_local_got_refcounts (i);
13171 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13172 if (elf_bad_symtab (i))
13173 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13175 locsymcount = symtab_hdr->sh_info;
13177 for (j = 0; j < locsymcount; ++j)
13179 if (local_got[j] > 0)
13181 local_got[j] = gotoff;
13182 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13185 local_got[j] = (bfd_vma) -1;
13189 /* Then the global .got entries. .plt refcounts are handled by
13190 adjust_dynamic_symbol */
13191 gofarg.gotoff = gotoff;
13192 gofarg.info = info;
13193 elf_link_hash_traverse (elf_hash_table (info),
13194 elf_gc_allocate_got_offsets,
13199 /* Many folk need no more in the way of final link than this, once
13200 got entry reference counting is enabled. */
13203 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13205 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13208 /* Invoke the regular ELF backend linker to do all the work. */
13209 return bfd_elf_final_link (abfd, info);
13213 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13215 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13217 if (rcookie->bad_symtab)
13218 rcookie->rel = rcookie->rels;
13220 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13222 unsigned long r_symndx;
13224 if (! rcookie->bad_symtab)
13225 if (rcookie->rel->r_offset > offset)
13227 if (rcookie->rel->r_offset != offset)
13230 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13231 if (r_symndx == STN_UNDEF)
13234 if (r_symndx >= rcookie->locsymcount
13235 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13237 struct elf_link_hash_entry *h;
13239 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13241 while (h->root.type == bfd_link_hash_indirect
13242 || h->root.type == bfd_link_hash_warning)
13243 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13245 if ((h->root.type == bfd_link_hash_defined
13246 || h->root.type == bfd_link_hash_defweak)
13247 && (h->root.u.def.section->owner != rcookie->abfd
13248 || h->root.u.def.section->kept_section != NULL
13249 || discarded_section (h->root.u.def.section)))
13254 /* It's not a relocation against a global symbol,
13255 but it could be a relocation against a local
13256 symbol for a discarded section. */
13258 Elf_Internal_Sym *isym;
13260 /* Need to: get the symbol; get the section. */
13261 isym = &rcookie->locsyms[r_symndx];
13262 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13264 && (isec->kept_section != NULL
13265 || discarded_section (isec)))
13273 /* Discard unneeded references to discarded sections.
13274 Returns -1 on error, 1 if any section's size was changed, 0 if
13275 nothing changed. This function assumes that the relocations are in
13276 sorted order, which is true for all known assemblers. */
13279 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13281 struct elf_reloc_cookie cookie;
13286 if (info->traditional_format
13287 || !is_elf_hash_table (info->hash))
13290 o = bfd_get_section_by_name (output_bfd, ".stab");
13295 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13298 || i->reloc_count == 0
13299 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13303 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13306 if (!init_reloc_cookie_for_section (&cookie, info, i))
13309 if (_bfd_discard_section_stabs (abfd, i,
13310 elf_section_data (i)->sec_info,
13311 bfd_elf_reloc_symbol_deleted_p,
13315 fini_reloc_cookie_for_section (&cookie, i);
13320 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13321 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13326 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13332 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13335 if (!init_reloc_cookie_for_section (&cookie, info, i))
13338 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13339 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13340 bfd_elf_reloc_symbol_deleted_p,
13344 fini_reloc_cookie_for_section (&cookie, i);
13348 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13350 const struct elf_backend_data *bed;
13352 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13355 bed = get_elf_backend_data (abfd);
13357 if (bed->elf_backend_discard_info != NULL)
13359 if (!init_reloc_cookie (&cookie, info, abfd))
13362 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13365 fini_reloc_cookie (&cookie, abfd);
13369 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13370 _bfd_elf_end_eh_frame_parsing (info);
13372 if (info->eh_frame_hdr_type
13373 && !bfd_link_relocatable (info)
13374 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13381 _bfd_elf_section_already_linked (bfd *abfd,
13383 struct bfd_link_info *info)
13386 const char *name, *key;
13387 struct bfd_section_already_linked *l;
13388 struct bfd_section_already_linked_hash_entry *already_linked_list;
13390 if (sec->output_section == bfd_abs_section_ptr)
13393 flags = sec->flags;
13395 /* Return if it isn't a linkonce section. A comdat group section
13396 also has SEC_LINK_ONCE set. */
13397 if ((flags & SEC_LINK_ONCE) == 0)
13400 /* Don't put group member sections on our list of already linked
13401 sections. They are handled as a group via their group section. */
13402 if (elf_sec_group (sec) != NULL)
13405 /* For a SHT_GROUP section, use the group signature as the key. */
13407 if ((flags & SEC_GROUP) != 0
13408 && elf_next_in_group (sec) != NULL
13409 && elf_group_name (elf_next_in_group (sec)) != NULL)
13410 key = elf_group_name (elf_next_in_group (sec));
13413 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13414 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13415 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13418 /* Must be a user linkonce section that doesn't follow gcc's
13419 naming convention. In this case we won't be matching
13420 single member groups. */
13424 already_linked_list = bfd_section_already_linked_table_lookup (key);
13426 for (l = already_linked_list->entry; l != NULL; l = l->next)
13428 /* We may have 2 different types of sections on the list: group
13429 sections with a signature of <key> (<key> is some string),
13430 and linkonce sections named .gnu.linkonce.<type>.<key>.
13431 Match like sections. LTO plugin sections are an exception.
13432 They are always named .gnu.linkonce.t.<key> and match either
13433 type of section. */
13434 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13435 && ((flags & SEC_GROUP) != 0
13436 || strcmp (name, l->sec->name) == 0))
13437 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13439 /* The section has already been linked. See if we should
13440 issue a warning. */
13441 if (!_bfd_handle_already_linked (sec, l, info))
13444 if (flags & SEC_GROUP)
13446 asection *first = elf_next_in_group (sec);
13447 asection *s = first;
13451 s->output_section = bfd_abs_section_ptr;
13452 /* Record which group discards it. */
13453 s->kept_section = l->sec;
13454 s = elf_next_in_group (s);
13455 /* These lists are circular. */
13465 /* A single member comdat group section may be discarded by a
13466 linkonce section and vice versa. */
13467 if ((flags & SEC_GROUP) != 0)
13469 asection *first = elf_next_in_group (sec);
13471 if (first != NULL && elf_next_in_group (first) == first)
13472 /* Check this single member group against linkonce sections. */
13473 for (l = already_linked_list->entry; l != NULL; l = l->next)
13474 if ((l->sec->flags & SEC_GROUP) == 0
13475 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13477 first->output_section = bfd_abs_section_ptr;
13478 first->kept_section = l->sec;
13479 sec->output_section = bfd_abs_section_ptr;
13484 /* Check this linkonce section against single member groups. */
13485 for (l = already_linked_list->entry; l != NULL; l = l->next)
13486 if (l->sec->flags & SEC_GROUP)
13488 asection *first = elf_next_in_group (l->sec);
13491 && elf_next_in_group (first) == first
13492 && bfd_elf_match_symbols_in_sections (first, sec, info))
13494 sec->output_section = bfd_abs_section_ptr;
13495 sec->kept_section = first;
13500 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13501 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13502 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13503 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13504 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13505 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13506 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13507 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13508 The reverse order cannot happen as there is never a bfd with only the
13509 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13510 matter as here were are looking only for cross-bfd sections. */
13512 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13513 for (l = already_linked_list->entry; l != NULL; l = l->next)
13514 if ((l->sec->flags & SEC_GROUP) == 0
13515 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13517 if (abfd != l->sec->owner)
13518 sec->output_section = bfd_abs_section_ptr;
13522 /* This is the first section with this name. Record it. */
13523 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13524 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13525 return sec->output_section == bfd_abs_section_ptr;
13529 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13531 return sym->st_shndx == SHN_COMMON;
13535 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13541 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13543 return bfd_com_section_ptr;
13547 _bfd_elf_default_got_elt_size (bfd *abfd,
13548 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13549 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13550 bfd *ibfd ATTRIBUTE_UNUSED,
13551 unsigned long symndx ATTRIBUTE_UNUSED)
13553 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13554 return bed->s->arch_size / 8;
13557 /* Routines to support the creation of dynamic relocs. */
13559 /* Returns the name of the dynamic reloc section associated with SEC. */
13561 static const char *
13562 get_dynamic_reloc_section_name (bfd * abfd,
13564 bfd_boolean is_rela)
13567 const char *old_name = bfd_get_section_name (NULL, sec);
13568 const char *prefix = is_rela ? ".rela" : ".rel";
13570 if (old_name == NULL)
13573 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13574 sprintf (name, "%s%s", prefix, old_name);
13579 /* Returns the dynamic reloc section associated with SEC.
13580 If necessary compute the name of the dynamic reloc section based
13581 on SEC's name (looked up in ABFD's string table) and the setting
13585 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13587 bfd_boolean is_rela)
13589 asection * reloc_sec = elf_section_data (sec)->sreloc;
13591 if (reloc_sec == NULL)
13593 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13597 reloc_sec = bfd_get_linker_section (abfd, name);
13599 if (reloc_sec != NULL)
13600 elf_section_data (sec)->sreloc = reloc_sec;
13607 /* Returns the dynamic reloc section associated with SEC. If the
13608 section does not exist it is created and attached to the DYNOBJ
13609 bfd and stored in the SRELOC field of SEC's elf_section_data
13612 ALIGNMENT is the alignment for the newly created section and
13613 IS_RELA defines whether the name should be .rela.<SEC's name>
13614 or .rel.<SEC's name>. The section name is looked up in the
13615 string table associated with ABFD. */
13618 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13620 unsigned int alignment,
13622 bfd_boolean is_rela)
13624 asection * reloc_sec = elf_section_data (sec)->sreloc;
13626 if (reloc_sec == NULL)
13628 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13633 reloc_sec = bfd_get_linker_section (dynobj, name);
13635 if (reloc_sec == NULL)
13637 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13638 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13639 if ((sec->flags & SEC_ALLOC) != 0)
13640 flags |= SEC_ALLOC | SEC_LOAD;
13642 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13643 if (reloc_sec != NULL)
13645 /* _bfd_elf_get_sec_type_attr chooses a section type by
13646 name. Override as it may be wrong, eg. for a user
13647 section named "auto" we'll get ".relauto" which is
13648 seen to be a .rela section. */
13649 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13650 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13655 elf_section_data (sec)->sreloc = reloc_sec;
13661 /* Copy the ELF symbol type and other attributes for a linker script
13662 assignment from HSRC to HDEST. Generally this should be treated as
13663 if we found a strong non-dynamic definition for HDEST (except that
13664 ld ignores multiple definition errors). */
13666 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13667 struct bfd_link_hash_entry *hdest,
13668 struct bfd_link_hash_entry *hsrc)
13670 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13671 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13672 Elf_Internal_Sym isym;
13674 ehdest->type = ehsrc->type;
13675 ehdest->target_internal = ehsrc->target_internal;
13677 isym.st_other = ehsrc->other;
13678 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13681 /* Append a RELA relocation REL to section S in BFD. */
13684 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13686 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13687 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13688 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13689 bed->s->swap_reloca_out (abfd, rel, loc);
13692 /* Append a REL relocation REL to section S in BFD. */
13695 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13697 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13698 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13699 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13700 bed->s->swap_reloc_out (abfd, rel, loc);