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. A common
1490 symbol in executable also overrides a symbol in a shared object. */
1495 || (h->root.type == bfd_link_hash_common
1498 || (!olddyn && bfd_link_executable (info))))))
1502 newdyncommon = FALSE;
1504 *psec = sec = bfd_und_section_ptr;
1505 *size_change_ok = TRUE;
1507 /* If we get here when the old symbol is a common symbol, then
1508 we are explicitly letting it override a weak symbol or
1509 function in a dynamic object, and we don't want to warn about
1510 a type change. If the old symbol is a defined symbol, a type
1511 change warning may still be appropriate. */
1513 if (h->root.type == bfd_link_hash_common)
1514 *type_change_ok = TRUE;
1517 /* Handle the special case of an old common symbol merging with a
1518 new symbol which looks like a common symbol in a shared object.
1519 We change *PSEC and *PVALUE to make the new symbol look like a
1520 common symbol, and let _bfd_generic_link_add_one_symbol do the
1524 && h->root.type == bfd_link_hash_common)
1528 newdyncommon = FALSE;
1529 *pvalue = sym->st_size;
1530 *psec = sec = bed->common_section (oldsec);
1531 *size_change_ok = TRUE;
1534 /* Skip weak definitions of symbols that are already defined. */
1535 if (newdef && olddef && newweak)
1537 /* Don't skip new non-IR weak syms. */
1538 if (!(oldbfd != NULL
1539 && (oldbfd->flags & BFD_PLUGIN) != 0
1540 && (abfd->flags & BFD_PLUGIN) == 0))
1546 /* Merge st_other. If the symbol already has a dynamic index,
1547 but visibility says it should not be visible, turn it into a
1549 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1550 if (h->dynindx != -1)
1551 switch (ELF_ST_VISIBILITY (h->other))
1555 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1560 /* If the old symbol is from a dynamic object, and the new symbol is
1561 a definition which is not from a dynamic object, then the new
1562 symbol overrides the old symbol. Symbols from regular files
1563 always take precedence over symbols from dynamic objects, even if
1564 they are defined after the dynamic object in the link.
1566 As above, we again permit a common symbol in a regular object to
1567 override a definition in a shared object if the shared object
1568 symbol is a function or is weak. */
1573 || (bfd_is_com_section (sec)
1574 && (oldweak || oldfunc)))
1579 /* Change the hash table entry to undefined, and let
1580 _bfd_generic_link_add_one_symbol do the right thing with the
1583 h->root.type = bfd_link_hash_undefined;
1584 h->root.u.undef.abfd = h->root.u.def.section->owner;
1585 *size_change_ok = TRUE;
1588 olddyncommon = FALSE;
1590 /* We again permit a type change when a common symbol may be
1591 overriding a function. */
1593 if (bfd_is_com_section (sec))
1597 /* If a common symbol overrides a function, make sure
1598 that it isn't defined dynamically nor has type
1601 h->type = STT_NOTYPE;
1603 *type_change_ok = TRUE;
1606 if (hi->root.type == bfd_link_hash_indirect)
1609 /* This union may have been set to be non-NULL when this symbol
1610 was seen in a dynamic object. We must force the union to be
1611 NULL, so that it is correct for a regular symbol. */
1612 h->verinfo.vertree = NULL;
1615 /* Handle the special case of a new common symbol merging with an
1616 old symbol that looks like it might be a common symbol defined in
1617 a shared object. Note that we have already handled the case in
1618 which a new common symbol should simply override the definition
1619 in the shared library. */
1622 && bfd_is_com_section (sec)
1625 /* It would be best if we could set the hash table entry to a
1626 common symbol, but we don't know what to use for the section
1627 or the alignment. */
1628 if (! ((*info->callbacks->multiple_common)
1629 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1632 /* If the presumed common symbol in the dynamic object is
1633 larger, pretend that the new symbol has its size. */
1635 if (h->size > *pvalue)
1638 /* We need to remember the alignment required by the symbol
1639 in the dynamic object. */
1640 BFD_ASSERT (pold_alignment);
1641 *pold_alignment = h->root.u.def.section->alignment_power;
1644 olddyncommon = FALSE;
1646 h->root.type = bfd_link_hash_undefined;
1647 h->root.u.undef.abfd = h->root.u.def.section->owner;
1649 *size_change_ok = TRUE;
1650 *type_change_ok = TRUE;
1652 if (hi->root.type == bfd_link_hash_indirect)
1655 h->verinfo.vertree = NULL;
1660 /* Handle the case where we had a versioned symbol in a dynamic
1661 library and now find a definition in a normal object. In this
1662 case, we make the versioned symbol point to the normal one. */
1663 flip->root.type = h->root.type;
1664 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1665 h->root.type = bfd_link_hash_indirect;
1666 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1667 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1671 flip->ref_dynamic = 1;
1678 /* This function is called to create an indirect symbol from the
1679 default for the symbol with the default version if needed. The
1680 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1681 set DYNSYM if the new indirect symbol is dynamic. */
1684 _bfd_elf_add_default_symbol (bfd *abfd,
1685 struct bfd_link_info *info,
1686 struct elf_link_hash_entry *h,
1688 Elf_Internal_Sym *sym,
1692 bfd_boolean *dynsym)
1694 bfd_boolean type_change_ok;
1695 bfd_boolean size_change_ok;
1698 struct elf_link_hash_entry *hi;
1699 struct bfd_link_hash_entry *bh;
1700 const struct elf_backend_data *bed;
1701 bfd_boolean collect;
1702 bfd_boolean dynamic;
1703 bfd_boolean override;
1705 size_t len, shortlen;
1707 bfd_boolean matched;
1709 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1712 /* If this symbol has a version, and it is the default version, we
1713 create an indirect symbol from the default name to the fully
1714 decorated name. This will cause external references which do not
1715 specify a version to be bound to this version of the symbol. */
1716 p = strchr (name, ELF_VER_CHR);
1717 if (h->versioned == unknown)
1721 h->versioned = unversioned;
1726 if (p[1] != ELF_VER_CHR)
1728 h->versioned = versioned_hidden;
1732 h->versioned = versioned;
1737 /* PR ld/19073: We may see an unversioned definition after the
1743 bed = get_elf_backend_data (abfd);
1744 collect = bed->collect;
1745 dynamic = (abfd->flags & DYNAMIC) != 0;
1747 shortlen = p - name;
1748 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1749 if (shortname == NULL)
1751 memcpy (shortname, name, shortlen);
1752 shortname[shortlen] = '\0';
1754 /* We are going to create a new symbol. Merge it with any existing
1755 symbol with this name. For the purposes of the merge, act as
1756 though we were defining the symbol we just defined, although we
1757 actually going to define an indirect symbol. */
1758 type_change_ok = FALSE;
1759 size_change_ok = FALSE;
1762 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1763 &hi, poldbfd, NULL, NULL, &skip, &override,
1764 &type_change_ok, &size_change_ok, &matched))
1772 /* Add the default symbol if not performing a relocatable link. */
1773 if (! bfd_link_relocatable (info))
1776 if (! (_bfd_generic_link_add_one_symbol
1777 (info, abfd, shortname, BSF_INDIRECT,
1778 bfd_ind_section_ptr,
1779 0, name, FALSE, collect, &bh)))
1781 hi = (struct elf_link_hash_entry *) bh;
1786 /* In this case the symbol named SHORTNAME is overriding the
1787 indirect symbol we want to add. We were planning on making
1788 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1789 is the name without a version. NAME is the fully versioned
1790 name, and it is the default version.
1792 Overriding means that we already saw a definition for the
1793 symbol SHORTNAME in a regular object, and it is overriding
1794 the symbol defined in the dynamic object.
1796 When this happens, we actually want to change NAME, the
1797 symbol we just added, to refer to SHORTNAME. This will cause
1798 references to NAME in the shared object to become references
1799 to SHORTNAME in the regular object. This is what we expect
1800 when we override a function in a shared object: that the
1801 references in the shared object will be mapped to the
1802 definition in the regular object. */
1804 while (hi->root.type == bfd_link_hash_indirect
1805 || hi->root.type == bfd_link_hash_warning)
1806 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1808 h->root.type = bfd_link_hash_indirect;
1809 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1813 hi->ref_dynamic = 1;
1817 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1822 /* Now set HI to H, so that the following code will set the
1823 other fields correctly. */
1827 /* Check if HI is a warning symbol. */
1828 if (hi->root.type == bfd_link_hash_warning)
1829 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1831 /* If there is a duplicate definition somewhere, then HI may not
1832 point to an indirect symbol. We will have reported an error to
1833 the user in that case. */
1835 if (hi->root.type == bfd_link_hash_indirect)
1837 struct elf_link_hash_entry *ht;
1839 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1840 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1842 /* A reference to the SHORTNAME symbol from a dynamic library
1843 will be satisfied by the versioned symbol at runtime. In
1844 effect, we have a reference to the versioned symbol. */
1845 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1846 hi->dynamic_def |= ht->dynamic_def;
1848 /* See if the new flags lead us to realize that the symbol must
1854 if (! bfd_link_executable (info)
1861 if (hi->ref_regular)
1867 /* We also need to define an indirection from the nondefault version
1871 len = strlen (name);
1872 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1873 if (shortname == NULL)
1875 memcpy (shortname, name, shortlen);
1876 memcpy (shortname + shortlen, p + 1, len - shortlen);
1878 /* Once again, merge with any existing symbol. */
1879 type_change_ok = FALSE;
1880 size_change_ok = FALSE;
1882 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1883 &hi, poldbfd, NULL, NULL, &skip, &override,
1884 &type_change_ok, &size_change_ok, &matched))
1892 /* Here SHORTNAME is a versioned name, so we don't expect to see
1893 the type of override we do in the case above unless it is
1894 overridden by a versioned definition. */
1895 if (hi->root.type != bfd_link_hash_defined
1896 && hi->root.type != bfd_link_hash_defweak)
1897 (*_bfd_error_handler)
1898 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1904 if (! (_bfd_generic_link_add_one_symbol
1905 (info, abfd, shortname, BSF_INDIRECT,
1906 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1908 hi = (struct elf_link_hash_entry *) bh;
1910 /* If there is a duplicate definition somewhere, then HI may not
1911 point to an indirect symbol. We will have reported an error
1912 to the user in that case. */
1914 if (hi->root.type == bfd_link_hash_indirect)
1916 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1917 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1918 hi->dynamic_def |= h->dynamic_def;
1920 /* See if the new flags lead us to realize that the symbol
1926 if (! bfd_link_executable (info)
1932 if (hi->ref_regular)
1942 /* This routine is used to export all defined symbols into the dynamic
1943 symbol table. It is called via elf_link_hash_traverse. */
1946 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1948 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1950 /* Ignore indirect symbols. These are added by the versioning code. */
1951 if (h->root.type == bfd_link_hash_indirect)
1954 /* Ignore this if we won't export it. */
1955 if (!eif->info->export_dynamic && !h->dynamic)
1958 if (h->dynindx == -1
1959 && (h->def_regular || h->ref_regular)
1960 && ! bfd_hide_sym_by_version (eif->info->version_info,
1961 h->root.root.string))
1963 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1973 /* Look through the symbols which are defined in other shared
1974 libraries and referenced here. Update the list of version
1975 dependencies. This will be put into the .gnu.version_r section.
1976 This function is called via elf_link_hash_traverse. */
1979 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1982 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1983 Elf_Internal_Verneed *t;
1984 Elf_Internal_Vernaux *a;
1987 /* We only care about symbols defined in shared objects with version
1992 || h->verinfo.verdef == NULL
1993 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1994 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
1997 /* See if we already know about this version. */
1998 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2002 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2005 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2006 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2012 /* This is a new version. Add it to tree we are building. */
2017 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2020 rinfo->failed = TRUE;
2024 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2025 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2026 elf_tdata (rinfo->info->output_bfd)->verref = t;
2030 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2033 rinfo->failed = TRUE;
2037 /* Note that we are copying a string pointer here, and testing it
2038 above. If bfd_elf_string_from_elf_section is ever changed to
2039 discard the string data when low in memory, this will have to be
2041 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2043 a->vna_flags = h->verinfo.verdef->vd_flags;
2044 a->vna_nextptr = t->vn_auxptr;
2046 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2049 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2056 /* Figure out appropriate versions for all the symbols. We may not
2057 have the version number script until we have read all of the input
2058 files, so until that point we don't know which symbols should be
2059 local. This function is called via elf_link_hash_traverse. */
2062 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2064 struct elf_info_failed *sinfo;
2065 struct bfd_link_info *info;
2066 const struct elf_backend_data *bed;
2067 struct elf_info_failed eif;
2071 sinfo = (struct elf_info_failed *) data;
2074 /* Fix the symbol flags. */
2077 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2080 sinfo->failed = TRUE;
2084 /* We only need version numbers for symbols defined in regular
2086 if (!h->def_regular)
2089 bed = get_elf_backend_data (info->output_bfd);
2090 p = strchr (h->root.root.string, ELF_VER_CHR);
2091 if (p != NULL && h->verinfo.vertree == NULL)
2093 struct bfd_elf_version_tree *t;
2096 if (*p == ELF_VER_CHR)
2099 /* If there is no version string, we can just return out. */
2103 /* Look for the version. If we find it, it is no longer weak. */
2104 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2106 if (strcmp (t->name, p) == 0)
2110 struct bfd_elf_version_expr *d;
2112 len = p - h->root.root.string;
2113 alc = (char *) bfd_malloc (len);
2116 sinfo->failed = TRUE;
2119 memcpy (alc, h->root.root.string, len - 1);
2120 alc[len - 1] = '\0';
2121 if (alc[len - 2] == ELF_VER_CHR)
2122 alc[len - 2] = '\0';
2124 h->verinfo.vertree = t;
2128 if (t->globals.list != NULL)
2129 d = (*t->match) (&t->globals, NULL, alc);
2131 /* See if there is anything to force this symbol to
2133 if (d == NULL && t->locals.list != NULL)
2135 d = (*t->match) (&t->locals, NULL, alc);
2138 && ! info->export_dynamic)
2139 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2147 /* If we are building an application, we need to create a
2148 version node for this version. */
2149 if (t == NULL && bfd_link_executable (info))
2151 struct bfd_elf_version_tree **pp;
2154 /* If we aren't going to export this symbol, we don't need
2155 to worry about it. */
2156 if (h->dynindx == -1)
2160 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2163 sinfo->failed = TRUE;
2168 t->name_indx = (unsigned int) -1;
2172 /* Don't count anonymous version tag. */
2173 if (sinfo->info->version_info != NULL
2174 && sinfo->info->version_info->vernum == 0)
2176 for (pp = &sinfo->info->version_info;
2180 t->vernum = version_index;
2184 h->verinfo.vertree = t;
2188 /* We could not find the version for a symbol when
2189 generating a shared archive. Return an error. */
2190 (*_bfd_error_handler)
2191 (_("%B: version node not found for symbol %s"),
2192 info->output_bfd, h->root.root.string);
2193 bfd_set_error (bfd_error_bad_value);
2194 sinfo->failed = TRUE;
2199 /* If we don't have a version for this symbol, see if we can find
2201 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2206 = bfd_find_version_for_sym (sinfo->info->version_info,
2207 h->root.root.string, &hide);
2208 if (h->verinfo.vertree != NULL && hide)
2209 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2215 /* Read and swap the relocs from the section indicated by SHDR. This
2216 may be either a REL or a RELA section. The relocations are
2217 translated into RELA relocations and stored in INTERNAL_RELOCS,
2218 which should have already been allocated to contain enough space.
2219 The EXTERNAL_RELOCS are a buffer where the external form of the
2220 relocations should be stored.
2222 Returns FALSE if something goes wrong. */
2225 elf_link_read_relocs_from_section (bfd *abfd,
2227 Elf_Internal_Shdr *shdr,
2228 void *external_relocs,
2229 Elf_Internal_Rela *internal_relocs)
2231 const struct elf_backend_data *bed;
2232 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2233 const bfd_byte *erela;
2234 const bfd_byte *erelaend;
2235 Elf_Internal_Rela *irela;
2236 Elf_Internal_Shdr *symtab_hdr;
2239 /* Position ourselves at the start of the section. */
2240 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2243 /* Read the relocations. */
2244 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2247 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2248 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2250 bed = get_elf_backend_data (abfd);
2252 /* Convert the external relocations to the internal format. */
2253 if (shdr->sh_entsize == bed->s->sizeof_rel)
2254 swap_in = bed->s->swap_reloc_in;
2255 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2256 swap_in = bed->s->swap_reloca_in;
2259 bfd_set_error (bfd_error_wrong_format);
2263 erela = (const bfd_byte *) external_relocs;
2264 erelaend = erela + shdr->sh_size;
2265 irela = internal_relocs;
2266 while (erela < erelaend)
2270 (*swap_in) (abfd, erela, irela);
2271 r_symndx = ELF32_R_SYM (irela->r_info);
2272 if (bed->s->arch_size == 64)
2276 if ((size_t) r_symndx >= nsyms)
2278 (*_bfd_error_handler)
2279 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2280 " for offset 0x%lx in section `%A'"),
2282 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2283 bfd_set_error (bfd_error_bad_value);
2287 else if (r_symndx != STN_UNDEF)
2289 (*_bfd_error_handler)
2290 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2291 " when the object file has no symbol table"),
2293 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2294 bfd_set_error (bfd_error_bad_value);
2297 irela += bed->s->int_rels_per_ext_rel;
2298 erela += shdr->sh_entsize;
2304 /* Read and swap the relocs for a section O. They may have been
2305 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2306 not NULL, they are used as buffers to read into. They are known to
2307 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2308 the return value is allocated using either malloc or bfd_alloc,
2309 according to the KEEP_MEMORY argument. If O has two relocation
2310 sections (both REL and RELA relocations), then the REL_HDR
2311 relocations will appear first in INTERNAL_RELOCS, followed by the
2312 RELA_HDR relocations. */
2315 _bfd_elf_link_read_relocs (bfd *abfd,
2317 void *external_relocs,
2318 Elf_Internal_Rela *internal_relocs,
2319 bfd_boolean keep_memory)
2321 void *alloc1 = NULL;
2322 Elf_Internal_Rela *alloc2 = NULL;
2323 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2324 struct bfd_elf_section_data *esdo = elf_section_data (o);
2325 Elf_Internal_Rela *internal_rela_relocs;
2327 if (esdo->relocs != NULL)
2328 return esdo->relocs;
2330 if (o->reloc_count == 0)
2333 if (internal_relocs == NULL)
2337 size = o->reloc_count;
2338 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2340 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2342 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2343 if (internal_relocs == NULL)
2347 if (external_relocs == NULL)
2349 bfd_size_type size = 0;
2352 size += esdo->rel.hdr->sh_size;
2354 size += esdo->rela.hdr->sh_size;
2356 alloc1 = bfd_malloc (size);
2359 external_relocs = alloc1;
2362 internal_rela_relocs = internal_relocs;
2365 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2369 external_relocs = (((bfd_byte *) external_relocs)
2370 + esdo->rel.hdr->sh_size);
2371 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2372 * bed->s->int_rels_per_ext_rel);
2376 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2378 internal_rela_relocs)))
2381 /* Cache the results for next time, if we can. */
2383 esdo->relocs = internal_relocs;
2388 /* Don't free alloc2, since if it was allocated we are passing it
2389 back (under the name of internal_relocs). */
2391 return internal_relocs;
2399 bfd_release (abfd, alloc2);
2406 /* Compute the size of, and allocate space for, REL_HDR which is the
2407 section header for a section containing relocations for O. */
2410 _bfd_elf_link_size_reloc_section (bfd *abfd,
2411 struct bfd_elf_section_reloc_data *reldata)
2413 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2415 /* That allows us to calculate the size of the section. */
2416 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2418 /* The contents field must last into write_object_contents, so we
2419 allocate it with bfd_alloc rather than malloc. Also since we
2420 cannot be sure that the contents will actually be filled in,
2421 we zero the allocated space. */
2422 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2423 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2426 if (reldata->hashes == NULL && reldata->count)
2428 struct elf_link_hash_entry **p;
2430 p = ((struct elf_link_hash_entry **)
2431 bfd_zmalloc (reldata->count * sizeof (*p)));
2435 reldata->hashes = p;
2441 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2442 originated from the section given by INPUT_REL_HDR) to the
2446 _bfd_elf_link_output_relocs (bfd *output_bfd,
2447 asection *input_section,
2448 Elf_Internal_Shdr *input_rel_hdr,
2449 Elf_Internal_Rela *internal_relocs,
2450 struct elf_link_hash_entry **rel_hash
2453 Elf_Internal_Rela *irela;
2454 Elf_Internal_Rela *irelaend;
2456 struct bfd_elf_section_reloc_data *output_reldata;
2457 asection *output_section;
2458 const struct elf_backend_data *bed;
2459 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2460 struct bfd_elf_section_data *esdo;
2462 output_section = input_section->output_section;
2464 bed = get_elf_backend_data (output_bfd);
2465 esdo = elf_section_data (output_section);
2466 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2468 output_reldata = &esdo->rel;
2469 swap_out = bed->s->swap_reloc_out;
2471 else if (esdo->rela.hdr
2472 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2474 output_reldata = &esdo->rela;
2475 swap_out = bed->s->swap_reloca_out;
2479 (*_bfd_error_handler)
2480 (_("%B: relocation size mismatch in %B section %A"),
2481 output_bfd, input_section->owner, input_section);
2482 bfd_set_error (bfd_error_wrong_format);
2486 erel = output_reldata->hdr->contents;
2487 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2488 irela = internal_relocs;
2489 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2490 * bed->s->int_rels_per_ext_rel);
2491 while (irela < irelaend)
2493 (*swap_out) (output_bfd, irela, erel);
2494 irela += bed->s->int_rels_per_ext_rel;
2495 erel += input_rel_hdr->sh_entsize;
2498 /* Bump the counter, so that we know where to add the next set of
2500 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2505 /* Make weak undefined symbols in PIE dynamic. */
2508 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2509 struct elf_link_hash_entry *h)
2511 if (bfd_link_pie (info)
2513 && h->root.type == bfd_link_hash_undefweak)
2514 return bfd_elf_link_record_dynamic_symbol (info, h);
2519 /* Fix up the flags for a symbol. This handles various cases which
2520 can only be fixed after all the input files are seen. This is
2521 currently called by both adjust_dynamic_symbol and
2522 assign_sym_version, which is unnecessary but perhaps more robust in
2523 the face of future changes. */
2526 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2527 struct elf_info_failed *eif)
2529 const struct elf_backend_data *bed;
2531 /* If this symbol was mentioned in a non-ELF file, try to set
2532 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2533 permit a non-ELF file to correctly refer to a symbol defined in
2534 an ELF dynamic object. */
2537 while (h->root.type == bfd_link_hash_indirect)
2538 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2540 if (h->root.type != bfd_link_hash_defined
2541 && h->root.type != bfd_link_hash_defweak)
2544 h->ref_regular_nonweak = 1;
2548 if (h->root.u.def.section->owner != NULL
2549 && (bfd_get_flavour (h->root.u.def.section->owner)
2550 == bfd_target_elf_flavour))
2553 h->ref_regular_nonweak = 1;
2559 if (h->dynindx == -1
2563 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2572 /* Unfortunately, NON_ELF is only correct if the symbol
2573 was first seen in a non-ELF file. Fortunately, if the symbol
2574 was first seen in an ELF file, we're probably OK unless the
2575 symbol was defined in a non-ELF file. Catch that case here.
2576 FIXME: We're still in trouble if the symbol was first seen in
2577 a dynamic object, and then later in a non-ELF regular object. */
2578 if ((h->root.type == bfd_link_hash_defined
2579 || h->root.type == bfd_link_hash_defweak)
2581 && (h->root.u.def.section->owner != NULL
2582 ? (bfd_get_flavour (h->root.u.def.section->owner)
2583 != bfd_target_elf_flavour)
2584 : (bfd_is_abs_section (h->root.u.def.section)
2585 && !h->def_dynamic)))
2589 /* Backend specific symbol fixup. */
2590 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2591 if (bed->elf_backend_fixup_symbol
2592 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2595 /* If this is a final link, and the symbol was defined as a common
2596 symbol in a regular object file, and there was no definition in
2597 any dynamic object, then the linker will have allocated space for
2598 the symbol in a common section but the DEF_REGULAR
2599 flag will not have been set. */
2600 if (h->root.type == bfd_link_hash_defined
2604 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2607 /* If -Bsymbolic was used (which means to bind references to global
2608 symbols to the definition within the shared object), and this
2609 symbol was defined in a regular object, then it actually doesn't
2610 need a PLT entry. Likewise, if the symbol has non-default
2611 visibility. If the symbol has hidden or internal visibility, we
2612 will force it local. */
2614 && bfd_link_pic (eif->info)
2615 && is_elf_hash_table (eif->info->hash)
2616 && (SYMBOLIC_BIND (eif->info, h)
2617 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2620 bfd_boolean force_local;
2622 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2623 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2624 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2627 /* If a weak undefined symbol has non-default visibility, we also
2628 hide it from the dynamic linker. */
2629 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2630 && h->root.type == bfd_link_hash_undefweak)
2631 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2633 /* If this is a weak defined symbol in a dynamic object, and we know
2634 the real definition in the dynamic object, copy interesting flags
2635 over to the real definition. */
2636 if (h->u.weakdef != NULL)
2638 /* If the real definition is defined by a regular object file,
2639 don't do anything special. See the longer description in
2640 _bfd_elf_adjust_dynamic_symbol, below. */
2641 if (h->u.weakdef->def_regular)
2642 h->u.weakdef = NULL;
2645 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2647 while (h->root.type == bfd_link_hash_indirect)
2648 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2650 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2651 || h->root.type == bfd_link_hash_defweak);
2652 BFD_ASSERT (weakdef->def_dynamic);
2653 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2654 || weakdef->root.type == bfd_link_hash_defweak);
2655 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2662 /* Make the backend pick a good value for a dynamic symbol. This is
2663 called via elf_link_hash_traverse, and also calls itself
2667 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2669 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2671 const struct elf_backend_data *bed;
2673 if (! is_elf_hash_table (eif->info->hash))
2676 /* Ignore indirect symbols. These are added by the versioning code. */
2677 if (h->root.type == bfd_link_hash_indirect)
2680 /* Fix the symbol flags. */
2681 if (! _bfd_elf_fix_symbol_flags (h, eif))
2684 /* If this symbol does not require a PLT entry, and it is not
2685 defined by a dynamic object, or is not referenced by a regular
2686 object, ignore it. We do have to handle a weak defined symbol,
2687 even if no regular object refers to it, if we decided to add it
2688 to the dynamic symbol table. FIXME: Do we normally need to worry
2689 about symbols which are defined by one dynamic object and
2690 referenced by another one? */
2692 && h->type != STT_GNU_IFUNC
2696 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2698 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2702 /* If we've already adjusted this symbol, don't do it again. This
2703 can happen via a recursive call. */
2704 if (h->dynamic_adjusted)
2707 /* Don't look at this symbol again. Note that we must set this
2708 after checking the above conditions, because we may look at a
2709 symbol once, decide not to do anything, and then get called
2710 recursively later after REF_REGULAR is set below. */
2711 h->dynamic_adjusted = 1;
2713 /* If this is a weak definition, and we know a real definition, and
2714 the real symbol is not itself defined by a regular object file,
2715 then get a good value for the real definition. We handle the
2716 real symbol first, for the convenience of the backend routine.
2718 Note that there is a confusing case here. If the real definition
2719 is defined by a regular object file, we don't get the real symbol
2720 from the dynamic object, but we do get the weak symbol. If the
2721 processor backend uses a COPY reloc, then if some routine in the
2722 dynamic object changes the real symbol, we will not see that
2723 change in the corresponding weak symbol. This is the way other
2724 ELF linkers work as well, and seems to be a result of the shared
2727 I will clarify this issue. Most SVR4 shared libraries define the
2728 variable _timezone and define timezone as a weak synonym. The
2729 tzset call changes _timezone. If you write
2730 extern int timezone;
2732 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2733 you might expect that, since timezone is a synonym for _timezone,
2734 the same number will print both times. However, if the processor
2735 backend uses a COPY reloc, then actually timezone will be copied
2736 into your process image, and, since you define _timezone
2737 yourself, _timezone will not. Thus timezone and _timezone will
2738 wind up at different memory locations. The tzset call will set
2739 _timezone, leaving timezone unchanged. */
2741 if (h->u.weakdef != NULL)
2743 /* If we get to this point, there is an implicit reference to
2744 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2745 h->u.weakdef->ref_regular = 1;
2747 /* Ensure that the backend adjust_dynamic_symbol function sees
2748 H->U.WEAKDEF before H by recursively calling ourselves. */
2749 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2753 /* If a symbol has no type and no size and does not require a PLT
2754 entry, then we are probably about to do the wrong thing here: we
2755 are probably going to create a COPY reloc for an empty object.
2756 This case can arise when a shared object is built with assembly
2757 code, and the assembly code fails to set the symbol type. */
2759 && h->type == STT_NOTYPE
2761 (*_bfd_error_handler)
2762 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2763 h->root.root.string);
2765 dynobj = elf_hash_table (eif->info)->dynobj;
2766 bed = get_elf_backend_data (dynobj);
2768 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2777 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2781 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2782 struct elf_link_hash_entry *h,
2785 unsigned int power_of_two;
2787 asection *sec = h->root.u.def.section;
2789 /* The section aligment of definition is the maximum alignment
2790 requirement of symbols defined in the section. Since we don't
2791 know the symbol alignment requirement, we start with the
2792 maximum alignment and check low bits of the symbol address
2793 for the minimum alignment. */
2794 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2795 mask = ((bfd_vma) 1 << power_of_two) - 1;
2796 while ((h->root.u.def.value & mask) != 0)
2802 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2805 /* Adjust the section alignment if needed. */
2806 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2811 /* We make sure that the symbol will be aligned properly. */
2812 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2814 /* Define the symbol as being at this point in DYNBSS. */
2815 h->root.u.def.section = dynbss;
2816 h->root.u.def.value = dynbss->size;
2818 /* Increment the size of DYNBSS to make room for the symbol. */
2819 dynbss->size += h->size;
2821 /* No error if extern_protected_data is true. */
2822 if (h->protected_def
2823 && (!info->extern_protected_data
2824 || (info->extern_protected_data < 0
2825 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2826 info->callbacks->einfo
2827 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2828 h->root.root.string);
2833 /* Adjust all external symbols pointing into SEC_MERGE sections
2834 to reflect the object merging within the sections. */
2837 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2841 if ((h->root.type == bfd_link_hash_defined
2842 || h->root.type == bfd_link_hash_defweak)
2843 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2844 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2846 bfd *output_bfd = (bfd *) data;
2848 h->root.u.def.value =
2849 _bfd_merged_section_offset (output_bfd,
2850 &h->root.u.def.section,
2851 elf_section_data (sec)->sec_info,
2852 h->root.u.def.value);
2858 /* Returns false if the symbol referred to by H should be considered
2859 to resolve local to the current module, and true if it should be
2860 considered to bind dynamically. */
2863 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2864 struct bfd_link_info *info,
2865 bfd_boolean not_local_protected)
2867 bfd_boolean binding_stays_local_p;
2868 const struct elf_backend_data *bed;
2869 struct elf_link_hash_table *hash_table;
2874 while (h->root.type == bfd_link_hash_indirect
2875 || h->root.type == bfd_link_hash_warning)
2876 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2878 /* If it was forced local, then clearly it's not dynamic. */
2879 if (h->dynindx == -1)
2881 if (h->forced_local)
2884 /* Identify the cases where name binding rules say that a
2885 visible symbol resolves locally. */
2886 binding_stays_local_p = (bfd_link_executable (info)
2887 || SYMBOLIC_BIND (info, h));
2889 switch (ELF_ST_VISIBILITY (h->other))
2896 hash_table = elf_hash_table (info);
2897 if (!is_elf_hash_table (hash_table))
2900 bed = get_elf_backend_data (hash_table->dynobj);
2902 /* Proper resolution for function pointer equality may require
2903 that these symbols perhaps be resolved dynamically, even though
2904 we should be resolving them to the current module. */
2905 if (!not_local_protected || !bed->is_function_type (h->type))
2906 binding_stays_local_p = TRUE;
2913 /* If it isn't defined locally, then clearly it's dynamic. */
2914 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2917 /* Otherwise, the symbol is dynamic if binding rules don't tell
2918 us that it remains local. */
2919 return !binding_stays_local_p;
2922 /* Return true if the symbol referred to by H should be considered
2923 to resolve local to the current module, and false otherwise. Differs
2924 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2925 undefined symbols. The two functions are virtually identical except
2926 for the place where forced_local and dynindx == -1 are tested. If
2927 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2928 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2929 the symbol is local only for defined symbols.
2930 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2931 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2932 treatment of undefined weak symbols. For those that do not make
2933 undefined weak symbols dynamic, both functions may return false. */
2936 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2937 struct bfd_link_info *info,
2938 bfd_boolean local_protected)
2940 const struct elf_backend_data *bed;
2941 struct elf_link_hash_table *hash_table;
2943 /* If it's a local sym, of course we resolve locally. */
2947 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2948 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2949 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2952 /* Common symbols that become definitions don't get the DEF_REGULAR
2953 flag set, so test it first, and don't bail out. */
2954 if (ELF_COMMON_DEF_P (h))
2956 /* If we don't have a definition in a regular file, then we can't
2957 resolve locally. The sym is either undefined or dynamic. */
2958 else if (!h->def_regular)
2961 /* Forced local symbols resolve locally. */
2962 if (h->forced_local)
2965 /* As do non-dynamic symbols. */
2966 if (h->dynindx == -1)
2969 /* At this point, we know the symbol is defined and dynamic. In an
2970 executable it must resolve locally, likewise when building symbolic
2971 shared libraries. */
2972 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
2975 /* Now deal with defined dynamic symbols in shared libraries. Ones
2976 with default visibility might not resolve locally. */
2977 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2980 hash_table = elf_hash_table (info);
2981 if (!is_elf_hash_table (hash_table))
2984 bed = get_elf_backend_data (hash_table->dynobj);
2986 /* If extern_protected_data is false, STV_PROTECTED non-function
2987 symbols are local. */
2988 if ((!info->extern_protected_data
2989 || (info->extern_protected_data < 0
2990 && !bed->extern_protected_data))
2991 && !bed->is_function_type (h->type))
2994 /* Function pointer equality tests may require that STV_PROTECTED
2995 symbols be treated as dynamic symbols. If the address of a
2996 function not defined in an executable is set to that function's
2997 plt entry in the executable, then the address of the function in
2998 a shared library must also be the plt entry in the executable. */
2999 return local_protected;
3002 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3003 aligned. Returns the first TLS output section. */
3005 struct bfd_section *
3006 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3008 struct bfd_section *sec, *tls;
3009 unsigned int align = 0;
3011 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3012 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3016 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3017 if (sec->alignment_power > align)
3018 align = sec->alignment_power;
3020 elf_hash_table (info)->tls_sec = tls;
3022 /* Ensure the alignment of the first section is the largest alignment,
3023 so that the tls segment starts aligned. */
3025 tls->alignment_power = align;
3030 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3032 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3033 Elf_Internal_Sym *sym)
3035 const struct elf_backend_data *bed;
3037 /* Local symbols do not count, but target specific ones might. */
3038 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3039 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3042 bed = get_elf_backend_data (abfd);
3043 /* Function symbols do not count. */
3044 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3047 /* If the section is undefined, then so is the symbol. */
3048 if (sym->st_shndx == SHN_UNDEF)
3051 /* If the symbol is defined in the common section, then
3052 it is a common definition and so does not count. */
3053 if (bed->common_definition (sym))
3056 /* If the symbol is in a target specific section then we
3057 must rely upon the backend to tell us what it is. */
3058 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3059 /* FIXME - this function is not coded yet:
3061 return _bfd_is_global_symbol_definition (abfd, sym);
3063 Instead for now assume that the definition is not global,
3064 Even if this is wrong, at least the linker will behave
3065 in the same way that it used to do. */
3071 /* Search the symbol table of the archive element of the archive ABFD
3072 whose archive map contains a mention of SYMDEF, and determine if
3073 the symbol is defined in this element. */
3075 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3077 Elf_Internal_Shdr * hdr;
3078 bfd_size_type symcount;
3079 bfd_size_type extsymcount;
3080 bfd_size_type extsymoff;
3081 Elf_Internal_Sym *isymbuf;
3082 Elf_Internal_Sym *isym;
3083 Elf_Internal_Sym *isymend;
3086 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3090 /* Return FALSE if the object has been claimed by plugin. */
3091 if (abfd->plugin_format == bfd_plugin_yes)
3094 if (! bfd_check_format (abfd, bfd_object))
3097 /* Select the appropriate symbol table. */
3098 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3099 hdr = &elf_tdata (abfd)->symtab_hdr;
3101 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3103 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3105 /* The sh_info field of the symtab header tells us where the
3106 external symbols start. We don't care about the local symbols. */
3107 if (elf_bad_symtab (abfd))
3109 extsymcount = symcount;
3114 extsymcount = symcount - hdr->sh_info;
3115 extsymoff = hdr->sh_info;
3118 if (extsymcount == 0)
3121 /* Read in the symbol table. */
3122 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3124 if (isymbuf == NULL)
3127 /* Scan the symbol table looking for SYMDEF. */
3129 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3133 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3138 if (strcmp (name, symdef->name) == 0)
3140 result = is_global_data_symbol_definition (abfd, isym);
3150 /* Add an entry to the .dynamic table. */
3153 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3157 struct elf_link_hash_table *hash_table;
3158 const struct elf_backend_data *bed;
3160 bfd_size_type newsize;
3161 bfd_byte *newcontents;
3162 Elf_Internal_Dyn dyn;
3164 hash_table = elf_hash_table (info);
3165 if (! is_elf_hash_table (hash_table))
3168 bed = get_elf_backend_data (hash_table->dynobj);
3169 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3170 BFD_ASSERT (s != NULL);
3172 newsize = s->size + bed->s->sizeof_dyn;
3173 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3174 if (newcontents == NULL)
3178 dyn.d_un.d_val = val;
3179 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3182 s->contents = newcontents;
3187 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3188 otherwise just check whether one already exists. Returns -1 on error,
3189 1 if a DT_NEEDED tag already exists, and 0 on success. */
3192 elf_add_dt_needed_tag (bfd *abfd,
3193 struct bfd_link_info *info,
3197 struct elf_link_hash_table *hash_table;
3198 bfd_size_type strindex;
3200 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3203 hash_table = elf_hash_table (info);
3204 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3205 if (strindex == (bfd_size_type) -1)
3208 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3211 const struct elf_backend_data *bed;
3214 bed = get_elf_backend_data (hash_table->dynobj);
3215 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3217 for (extdyn = sdyn->contents;
3218 extdyn < sdyn->contents + sdyn->size;
3219 extdyn += bed->s->sizeof_dyn)
3221 Elf_Internal_Dyn dyn;
3223 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3224 if (dyn.d_tag == DT_NEEDED
3225 && dyn.d_un.d_val == strindex)
3227 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3235 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3238 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3242 /* We were just checking for existence of the tag. */
3243 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3248 /* Return true if SONAME is on the needed list between NEEDED and STOP
3249 (or the end of list if STOP is NULL), and needed by a library that
3253 on_needed_list (const char *soname,
3254 struct bfd_link_needed_list *needed,
3255 struct bfd_link_needed_list *stop)
3257 struct bfd_link_needed_list *look;
3258 for (look = needed; look != stop; look = look->next)
3259 if (strcmp (soname, look->name) == 0
3260 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3261 /* If needed by a library that itself is not directly
3262 needed, recursively check whether that library is
3263 indirectly needed. Since we add DT_NEEDED entries to
3264 the end of the list, library dependencies appear after
3265 the library. Therefore search prior to the current
3266 LOOK, preventing possible infinite recursion. */
3267 || on_needed_list (elf_dt_name (look->by), needed, look)))
3273 /* Sort symbol by value, section, and size. */
3275 elf_sort_symbol (const void *arg1, const void *arg2)
3277 const struct elf_link_hash_entry *h1;
3278 const struct elf_link_hash_entry *h2;
3279 bfd_signed_vma vdiff;
3281 h1 = *(const struct elf_link_hash_entry **) arg1;
3282 h2 = *(const struct elf_link_hash_entry **) arg2;
3283 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3285 return vdiff > 0 ? 1 : -1;
3288 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3290 return sdiff > 0 ? 1 : -1;
3292 vdiff = h1->size - h2->size;
3293 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3296 /* This function is used to adjust offsets into .dynstr for
3297 dynamic symbols. This is called via elf_link_hash_traverse. */
3300 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3302 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3304 if (h->dynindx != -1)
3305 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3309 /* Assign string offsets in .dynstr, update all structures referencing
3313 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3315 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3316 struct elf_link_local_dynamic_entry *entry;
3317 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3318 bfd *dynobj = hash_table->dynobj;
3321 const struct elf_backend_data *bed;
3324 _bfd_elf_strtab_finalize (dynstr);
3325 size = _bfd_elf_strtab_size (dynstr);
3327 bed = get_elf_backend_data (dynobj);
3328 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3329 BFD_ASSERT (sdyn != NULL);
3331 /* Update all .dynamic entries referencing .dynstr strings. */
3332 for (extdyn = sdyn->contents;
3333 extdyn < sdyn->contents + sdyn->size;
3334 extdyn += bed->s->sizeof_dyn)
3336 Elf_Internal_Dyn dyn;
3338 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3342 dyn.d_un.d_val = size;
3352 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3357 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3360 /* Now update local dynamic symbols. */
3361 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3362 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3363 entry->isym.st_name);
3365 /* And the rest of dynamic symbols. */
3366 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3368 /* Adjust version definitions. */
3369 if (elf_tdata (output_bfd)->cverdefs)
3374 Elf_Internal_Verdef def;
3375 Elf_Internal_Verdaux defaux;
3377 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3381 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3383 p += sizeof (Elf_External_Verdef);
3384 if (def.vd_aux != sizeof (Elf_External_Verdef))
3386 for (i = 0; i < def.vd_cnt; ++i)
3388 _bfd_elf_swap_verdaux_in (output_bfd,
3389 (Elf_External_Verdaux *) p, &defaux);
3390 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3392 _bfd_elf_swap_verdaux_out (output_bfd,
3393 &defaux, (Elf_External_Verdaux *) p);
3394 p += sizeof (Elf_External_Verdaux);
3397 while (def.vd_next);
3400 /* Adjust version references. */
3401 if (elf_tdata (output_bfd)->verref)
3406 Elf_Internal_Verneed need;
3407 Elf_Internal_Vernaux needaux;
3409 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3413 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3415 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3416 _bfd_elf_swap_verneed_out (output_bfd, &need,
3417 (Elf_External_Verneed *) p);
3418 p += sizeof (Elf_External_Verneed);
3419 for (i = 0; i < need.vn_cnt; ++i)
3421 _bfd_elf_swap_vernaux_in (output_bfd,
3422 (Elf_External_Vernaux *) p, &needaux);
3423 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3425 _bfd_elf_swap_vernaux_out (output_bfd,
3427 (Elf_External_Vernaux *) p);
3428 p += sizeof (Elf_External_Vernaux);
3431 while (need.vn_next);
3437 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3438 The default is to only match when the INPUT and OUTPUT are exactly
3442 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3443 const bfd_target *output)
3445 return input == output;
3448 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3449 This version is used when different targets for the same architecture
3450 are virtually identical. */
3453 _bfd_elf_relocs_compatible (const bfd_target *input,
3454 const bfd_target *output)
3456 const struct elf_backend_data *obed, *ibed;
3458 if (input == output)
3461 ibed = xvec_get_elf_backend_data (input);
3462 obed = xvec_get_elf_backend_data (output);
3464 if (ibed->arch != obed->arch)
3467 /* If both backends are using this function, deem them compatible. */
3468 return ibed->relocs_compatible == obed->relocs_compatible;
3471 /* Make a special call to the linker "notice" function to tell it that
3472 we are about to handle an as-needed lib, or have finished
3473 processing the lib. */
3476 _bfd_elf_notice_as_needed (bfd *ibfd,
3477 struct bfd_link_info *info,
3478 enum notice_asneeded_action act)
3480 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3483 /* Add symbols from an ELF object file to the linker hash table. */
3486 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3488 Elf_Internal_Ehdr *ehdr;
3489 Elf_Internal_Shdr *hdr;
3490 bfd_size_type symcount;
3491 bfd_size_type extsymcount;
3492 bfd_size_type extsymoff;
3493 struct elf_link_hash_entry **sym_hash;
3494 bfd_boolean dynamic;
3495 Elf_External_Versym *extversym = NULL;
3496 Elf_External_Versym *ever;
3497 struct elf_link_hash_entry *weaks;
3498 struct elf_link_hash_entry **nondeflt_vers = NULL;
3499 bfd_size_type nondeflt_vers_cnt = 0;
3500 Elf_Internal_Sym *isymbuf = NULL;
3501 Elf_Internal_Sym *isym;
3502 Elf_Internal_Sym *isymend;
3503 const struct elf_backend_data *bed;
3504 bfd_boolean add_needed;
3505 struct elf_link_hash_table *htab;
3507 void *alloc_mark = NULL;
3508 struct bfd_hash_entry **old_table = NULL;
3509 unsigned int old_size = 0;
3510 unsigned int old_count = 0;
3511 void *old_tab = NULL;
3513 struct bfd_link_hash_entry *old_undefs = NULL;
3514 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3515 long old_dynsymcount = 0;
3516 bfd_size_type old_dynstr_size = 0;
3519 bfd_boolean just_syms;
3521 htab = elf_hash_table (info);
3522 bed = get_elf_backend_data (abfd);
3524 if ((abfd->flags & DYNAMIC) == 0)
3530 /* You can't use -r against a dynamic object. Also, there's no
3531 hope of using a dynamic object which does not exactly match
3532 the format of the output file. */
3533 if (bfd_link_relocatable (info)
3534 || !is_elf_hash_table (htab)
3535 || info->output_bfd->xvec != abfd->xvec)
3537 if (bfd_link_relocatable (info))
3538 bfd_set_error (bfd_error_invalid_operation);
3540 bfd_set_error (bfd_error_wrong_format);
3545 ehdr = elf_elfheader (abfd);
3546 if (info->warn_alternate_em
3547 && bed->elf_machine_code != ehdr->e_machine
3548 && ((bed->elf_machine_alt1 != 0
3549 && ehdr->e_machine == bed->elf_machine_alt1)
3550 || (bed->elf_machine_alt2 != 0
3551 && ehdr->e_machine == bed->elf_machine_alt2)))
3552 info->callbacks->einfo
3553 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3554 ehdr->e_machine, abfd, bed->elf_machine_code);
3556 /* As a GNU extension, any input sections which are named
3557 .gnu.warning.SYMBOL are treated as warning symbols for the given
3558 symbol. This differs from .gnu.warning sections, which generate
3559 warnings when they are included in an output file. */
3560 /* PR 12761: Also generate this warning when building shared libraries. */
3561 for (s = abfd->sections; s != NULL; s = s->next)
3565 name = bfd_get_section_name (abfd, s);
3566 if (CONST_STRNEQ (name, ".gnu.warning."))
3571 name += sizeof ".gnu.warning." - 1;
3573 /* If this is a shared object, then look up the symbol
3574 in the hash table. If it is there, and it is already
3575 been defined, then we will not be using the entry
3576 from this shared object, so we don't need to warn.
3577 FIXME: If we see the definition in a regular object
3578 later on, we will warn, but we shouldn't. The only
3579 fix is to keep track of what warnings we are supposed
3580 to emit, and then handle them all at the end of the
3584 struct elf_link_hash_entry *h;
3586 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3588 /* FIXME: What about bfd_link_hash_common? */
3590 && (h->root.type == bfd_link_hash_defined
3591 || h->root.type == bfd_link_hash_defweak))
3596 msg = (char *) bfd_alloc (abfd, sz + 1);
3600 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3605 if (! (_bfd_generic_link_add_one_symbol
3606 (info, abfd, name, BSF_WARNING, s, 0, msg,
3607 FALSE, bed->collect, NULL)))
3610 if (bfd_link_executable (info))
3612 /* Clobber the section size so that the warning does
3613 not get copied into the output file. */
3616 /* Also set SEC_EXCLUDE, so that symbols defined in
3617 the warning section don't get copied to the output. */
3618 s->flags |= SEC_EXCLUDE;
3623 just_syms = ((s = abfd->sections) != NULL
3624 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3629 /* If we are creating a shared library, create all the dynamic
3630 sections immediately. We need to attach them to something,
3631 so we attach them to this BFD, provided it is the right
3632 format and is not from ld --just-symbols. Always create the
3633 dynamic sections for -E/--dynamic-list. FIXME: If there
3634 are no input BFD's of the same format as the output, we can't
3635 make a shared library. */
3637 && (bfd_link_pic (info)
3638 || (!bfd_link_relocatable (info)
3639 && (info->export_dynamic || info->dynamic)))
3640 && is_elf_hash_table (htab)
3641 && info->output_bfd->xvec == abfd->xvec
3642 && !htab->dynamic_sections_created)
3644 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3648 else if (!is_elf_hash_table (htab))
3652 const char *soname = NULL;
3654 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3657 /* ld --just-symbols and dynamic objects don't mix very well.
3658 ld shouldn't allow it. */
3662 /* If this dynamic lib was specified on the command line with
3663 --as-needed in effect, then we don't want to add a DT_NEEDED
3664 tag unless the lib is actually used. Similary for libs brought
3665 in by another lib's DT_NEEDED. When --no-add-needed is used
3666 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3667 any dynamic library in DT_NEEDED tags in the dynamic lib at
3669 add_needed = (elf_dyn_lib_class (abfd)
3670 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3671 | DYN_NO_NEEDED)) == 0;
3673 s = bfd_get_section_by_name (abfd, ".dynamic");
3678 unsigned int elfsec;
3679 unsigned long shlink;
3681 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3688 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3689 if (elfsec == SHN_BAD)
3690 goto error_free_dyn;
3691 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3693 for (extdyn = dynbuf;
3694 extdyn < dynbuf + s->size;
3695 extdyn += bed->s->sizeof_dyn)
3697 Elf_Internal_Dyn dyn;
3699 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3700 if (dyn.d_tag == DT_SONAME)
3702 unsigned int tagv = dyn.d_un.d_val;
3703 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3705 goto error_free_dyn;
3707 if (dyn.d_tag == DT_NEEDED)
3709 struct bfd_link_needed_list *n, **pn;
3711 unsigned int tagv = dyn.d_un.d_val;
3713 amt = sizeof (struct bfd_link_needed_list);
3714 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3715 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3716 if (n == NULL || fnm == NULL)
3717 goto error_free_dyn;
3718 amt = strlen (fnm) + 1;
3719 anm = (char *) bfd_alloc (abfd, amt);
3721 goto error_free_dyn;
3722 memcpy (anm, fnm, amt);
3726 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3730 if (dyn.d_tag == DT_RUNPATH)
3732 struct bfd_link_needed_list *n, **pn;
3734 unsigned int tagv = dyn.d_un.d_val;
3736 amt = sizeof (struct bfd_link_needed_list);
3737 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3738 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3739 if (n == NULL || fnm == NULL)
3740 goto error_free_dyn;
3741 amt = strlen (fnm) + 1;
3742 anm = (char *) bfd_alloc (abfd, amt);
3744 goto error_free_dyn;
3745 memcpy (anm, fnm, amt);
3749 for (pn = & runpath;
3755 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3756 if (!runpath && dyn.d_tag == DT_RPATH)
3758 struct bfd_link_needed_list *n, **pn;
3760 unsigned int tagv = dyn.d_un.d_val;
3762 amt = sizeof (struct bfd_link_needed_list);
3763 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3764 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3765 if (n == NULL || fnm == NULL)
3766 goto error_free_dyn;
3767 amt = strlen (fnm) + 1;
3768 anm = (char *) bfd_alloc (abfd, amt);
3770 goto error_free_dyn;
3771 memcpy (anm, fnm, amt);
3781 if (dyn.d_tag == DT_AUDIT)
3783 unsigned int tagv = dyn.d_un.d_val;
3784 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3791 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3792 frees all more recently bfd_alloc'd blocks as well. */
3798 struct bfd_link_needed_list **pn;
3799 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3804 /* We do not want to include any of the sections in a dynamic
3805 object in the output file. We hack by simply clobbering the
3806 list of sections in the BFD. This could be handled more
3807 cleanly by, say, a new section flag; the existing
3808 SEC_NEVER_LOAD flag is not the one we want, because that one
3809 still implies that the section takes up space in the output
3811 bfd_section_list_clear (abfd);
3813 /* Find the name to use in a DT_NEEDED entry that refers to this
3814 object. If the object has a DT_SONAME entry, we use it.
3815 Otherwise, if the generic linker stuck something in
3816 elf_dt_name, we use that. Otherwise, we just use the file
3818 if (soname == NULL || *soname == '\0')
3820 soname = elf_dt_name (abfd);
3821 if (soname == NULL || *soname == '\0')
3822 soname = bfd_get_filename (abfd);
3825 /* Save the SONAME because sometimes the linker emulation code
3826 will need to know it. */
3827 elf_dt_name (abfd) = soname;
3829 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3833 /* If we have already included this dynamic object in the
3834 link, just ignore it. There is no reason to include a
3835 particular dynamic object more than once. */
3839 /* Save the DT_AUDIT entry for the linker emulation code. */
3840 elf_dt_audit (abfd) = audit;
3843 /* If this is a dynamic object, we always link against the .dynsym
3844 symbol table, not the .symtab symbol table. The dynamic linker
3845 will only see the .dynsym symbol table, so there is no reason to
3846 look at .symtab for a dynamic object. */
3848 if (! dynamic || elf_dynsymtab (abfd) == 0)
3849 hdr = &elf_tdata (abfd)->symtab_hdr;
3851 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3853 symcount = hdr->sh_size / bed->s->sizeof_sym;
3855 /* The sh_info field of the symtab header tells us where the
3856 external symbols start. We don't care about the local symbols at
3858 if (elf_bad_symtab (abfd))
3860 extsymcount = symcount;
3865 extsymcount = symcount - hdr->sh_info;
3866 extsymoff = hdr->sh_info;
3869 sym_hash = elf_sym_hashes (abfd);
3870 if (extsymcount != 0)
3872 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3874 if (isymbuf == NULL)
3877 if (sym_hash == NULL)
3879 /* We store a pointer to the hash table entry for each
3881 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3882 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3883 if (sym_hash == NULL)
3884 goto error_free_sym;
3885 elf_sym_hashes (abfd) = sym_hash;
3891 /* Read in any version definitions. */
3892 if (!_bfd_elf_slurp_version_tables (abfd,
3893 info->default_imported_symver))
3894 goto error_free_sym;
3896 /* Read in the symbol versions, but don't bother to convert them
3897 to internal format. */
3898 if (elf_dynversym (abfd) != 0)
3900 Elf_Internal_Shdr *versymhdr;
3902 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3903 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3904 if (extversym == NULL)
3905 goto error_free_sym;
3906 amt = versymhdr->sh_size;
3907 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3908 || bfd_bread (extversym, amt, abfd) != amt)
3909 goto error_free_vers;
3913 /* If we are loading an as-needed shared lib, save the symbol table
3914 state before we start adding symbols. If the lib turns out
3915 to be unneeded, restore the state. */
3916 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3921 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3923 struct bfd_hash_entry *p;
3924 struct elf_link_hash_entry *h;
3926 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3928 h = (struct elf_link_hash_entry *) p;
3929 entsize += htab->root.table.entsize;
3930 if (h->root.type == bfd_link_hash_warning)
3931 entsize += htab->root.table.entsize;
3935 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3936 old_tab = bfd_malloc (tabsize + entsize);
3937 if (old_tab == NULL)
3938 goto error_free_vers;
3940 /* Remember the current objalloc pointer, so that all mem for
3941 symbols added can later be reclaimed. */
3942 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3943 if (alloc_mark == NULL)
3944 goto error_free_vers;
3946 /* Make a special call to the linker "notice" function to
3947 tell it that we are about to handle an as-needed lib. */
3948 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
3949 goto error_free_vers;
3951 /* Clone the symbol table. Remember some pointers into the
3952 symbol table, and dynamic symbol count. */
3953 old_ent = (char *) old_tab + tabsize;
3954 memcpy (old_tab, htab->root.table.table, tabsize);
3955 old_undefs = htab->root.undefs;
3956 old_undefs_tail = htab->root.undefs_tail;
3957 old_table = htab->root.table.table;
3958 old_size = htab->root.table.size;
3959 old_count = htab->root.table.count;
3960 old_dynsymcount = htab->dynsymcount;
3961 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
3963 for (i = 0; i < htab->root.table.size; i++)
3965 struct bfd_hash_entry *p;
3966 struct elf_link_hash_entry *h;
3968 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3970 memcpy (old_ent, p, htab->root.table.entsize);
3971 old_ent = (char *) old_ent + htab->root.table.entsize;
3972 h = (struct elf_link_hash_entry *) p;
3973 if (h->root.type == bfd_link_hash_warning)
3975 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3976 old_ent = (char *) old_ent + htab->root.table.entsize;
3983 ever = extversym != NULL ? extversym + extsymoff : NULL;
3984 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3986 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3990 asection *sec, *new_sec;
3993 struct elf_link_hash_entry *h;
3994 struct elf_link_hash_entry *hi;
3995 bfd_boolean definition;
3996 bfd_boolean size_change_ok;
3997 bfd_boolean type_change_ok;
3998 bfd_boolean new_weakdef;
3999 bfd_boolean new_weak;
4000 bfd_boolean old_weak;
4001 bfd_boolean override;
4003 unsigned int old_alignment;
4005 bfd_boolean matched;
4009 flags = BSF_NO_FLAGS;
4011 value = isym->st_value;
4012 common = bed->common_definition (isym);
4014 bind = ELF_ST_BIND (isym->st_info);
4018 /* This should be impossible, since ELF requires that all
4019 global symbols follow all local symbols, and that sh_info
4020 point to the first global symbol. Unfortunately, Irix 5
4025 if (isym->st_shndx != SHN_UNDEF && !common)
4033 case STB_GNU_UNIQUE:
4034 flags = BSF_GNU_UNIQUE;
4038 /* Leave it up to the processor backend. */
4042 if (isym->st_shndx == SHN_UNDEF)
4043 sec = bfd_und_section_ptr;
4044 else if (isym->st_shndx == SHN_ABS)
4045 sec = bfd_abs_section_ptr;
4046 else if (isym->st_shndx == SHN_COMMON)
4048 sec = bfd_com_section_ptr;
4049 /* What ELF calls the size we call the value. What ELF
4050 calls the value we call the alignment. */
4051 value = isym->st_size;
4055 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4057 sec = bfd_abs_section_ptr;
4058 else if (discarded_section (sec))
4060 /* Symbols from discarded section are undefined. We keep
4062 sec = bfd_und_section_ptr;
4063 isym->st_shndx = SHN_UNDEF;
4065 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4069 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4072 goto error_free_vers;
4074 if (isym->st_shndx == SHN_COMMON
4075 && (abfd->flags & BFD_PLUGIN) != 0)
4077 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4081 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4083 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4085 goto error_free_vers;
4089 else if (isym->st_shndx == SHN_COMMON
4090 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4091 && !bfd_link_relocatable (info))
4093 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4097 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4098 | SEC_LINKER_CREATED);
4099 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4101 goto error_free_vers;
4105 else if (bed->elf_add_symbol_hook)
4107 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4109 goto error_free_vers;
4111 /* The hook function sets the name to NULL if this symbol
4112 should be skipped for some reason. */
4117 /* Sanity check that all possibilities were handled. */
4120 bfd_set_error (bfd_error_bad_value);
4121 goto error_free_vers;
4124 /* Silently discard TLS symbols from --just-syms. There's
4125 no way to combine a static TLS block with a new TLS block
4126 for this executable. */
4127 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4128 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4131 if (bfd_is_und_section (sec)
4132 || bfd_is_com_section (sec))
4137 size_change_ok = FALSE;
4138 type_change_ok = bed->type_change_ok;
4145 if (is_elf_hash_table (htab))
4147 Elf_Internal_Versym iver;
4148 unsigned int vernum = 0;
4153 if (info->default_imported_symver)
4154 /* Use the default symbol version created earlier. */
4155 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4160 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4162 vernum = iver.vs_vers & VERSYM_VERSION;
4164 /* If this is a hidden symbol, or if it is not version
4165 1, we append the version name to the symbol name.
4166 However, we do not modify a non-hidden absolute symbol
4167 if it is not a function, because it might be the version
4168 symbol itself. FIXME: What if it isn't? */
4169 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4171 && (!bfd_is_abs_section (sec)
4172 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4175 size_t namelen, verlen, newlen;
4178 if (isym->st_shndx != SHN_UNDEF)
4180 if (vernum > elf_tdata (abfd)->cverdefs)
4182 else if (vernum > 1)
4184 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4190 (*_bfd_error_handler)
4191 (_("%B: %s: invalid version %u (max %d)"),
4193 elf_tdata (abfd)->cverdefs);
4194 bfd_set_error (bfd_error_bad_value);
4195 goto error_free_vers;
4200 /* We cannot simply test for the number of
4201 entries in the VERNEED section since the
4202 numbers for the needed versions do not start
4204 Elf_Internal_Verneed *t;
4207 for (t = elf_tdata (abfd)->verref;
4211 Elf_Internal_Vernaux *a;
4213 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4215 if (a->vna_other == vernum)
4217 verstr = a->vna_nodename;
4226 (*_bfd_error_handler)
4227 (_("%B: %s: invalid needed version %d"),
4228 abfd, name, vernum);
4229 bfd_set_error (bfd_error_bad_value);
4230 goto error_free_vers;
4234 namelen = strlen (name);
4235 verlen = strlen (verstr);
4236 newlen = namelen + verlen + 2;
4237 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4238 && isym->st_shndx != SHN_UNDEF)
4241 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4242 if (newname == NULL)
4243 goto error_free_vers;
4244 memcpy (newname, name, namelen);
4245 p = newname + namelen;
4247 /* If this is a defined non-hidden version symbol,
4248 we add another @ to the name. This indicates the
4249 default version of the symbol. */
4250 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4251 && isym->st_shndx != SHN_UNDEF)
4253 memcpy (p, verstr, verlen + 1);
4258 /* If this symbol has default visibility and the user has
4259 requested we not re-export it, then mark it as hidden. */
4260 if (!bfd_is_und_section (sec)
4263 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4264 isym->st_other = (STV_HIDDEN
4265 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4267 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4268 sym_hash, &old_bfd, &old_weak,
4269 &old_alignment, &skip, &override,
4270 &type_change_ok, &size_change_ok,
4272 goto error_free_vers;
4277 /* Override a definition only if the new symbol matches the
4279 if (override && matched)
4283 while (h->root.type == bfd_link_hash_indirect
4284 || h->root.type == bfd_link_hash_warning)
4285 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4287 if (elf_tdata (abfd)->verdef != NULL
4290 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4293 if (! (_bfd_generic_link_add_one_symbol
4294 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4295 (struct bfd_link_hash_entry **) sym_hash)))
4296 goto error_free_vers;
4299 /* We need to make sure that indirect symbol dynamic flags are
4302 while (h->root.type == bfd_link_hash_indirect
4303 || h->root.type == bfd_link_hash_warning)
4304 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4308 new_weak = (flags & BSF_WEAK) != 0;
4309 new_weakdef = FALSE;
4313 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4314 && is_elf_hash_table (htab)
4315 && h->u.weakdef == NULL)
4317 /* Keep a list of all weak defined non function symbols from
4318 a dynamic object, using the weakdef field. Later in this
4319 function we will set the weakdef field to the correct
4320 value. We only put non-function symbols from dynamic
4321 objects on this list, because that happens to be the only
4322 time we need to know the normal symbol corresponding to a
4323 weak symbol, and the information is time consuming to
4324 figure out. If the weakdef field is not already NULL,
4325 then this symbol was already defined by some previous
4326 dynamic object, and we will be using that previous
4327 definition anyhow. */
4329 h->u.weakdef = weaks;
4334 /* Set the alignment of a common symbol. */
4335 if ((common || bfd_is_com_section (sec))
4336 && h->root.type == bfd_link_hash_common)
4341 align = bfd_log2 (isym->st_value);
4344 /* The new symbol is a common symbol in a shared object.
4345 We need to get the alignment from the section. */
4346 align = new_sec->alignment_power;
4348 if (align > old_alignment)
4349 h->root.u.c.p->alignment_power = align;
4351 h->root.u.c.p->alignment_power = old_alignment;
4354 if (is_elf_hash_table (htab))
4356 /* Set a flag in the hash table entry indicating the type of
4357 reference or definition we just found. A dynamic symbol
4358 is one which is referenced or defined by both a regular
4359 object and a shared object. */
4360 bfd_boolean dynsym = FALSE;
4362 /* Plugin symbols aren't normal. Don't set def_regular or
4363 ref_regular for them, or make them dynamic. */
4364 if ((abfd->flags & BFD_PLUGIN) != 0)
4371 if (bind != STB_WEAK)
4372 h->ref_regular_nonweak = 1;
4384 /* If the indirect symbol has been forced local, don't
4385 make the real symbol dynamic. */
4386 if ((h == hi || !hi->forced_local)
4387 && (bfd_link_dll (info)
4397 hi->ref_dynamic = 1;
4402 hi->def_dynamic = 1;
4405 /* If the indirect symbol has been forced local, don't
4406 make the real symbol dynamic. */
4407 if ((h == hi || !hi->forced_local)
4410 || (h->u.weakdef != NULL
4412 && h->u.weakdef->dynindx != -1)))
4416 /* Check to see if we need to add an indirect symbol for
4417 the default name. */
4419 || (!override && h->root.type == bfd_link_hash_common))
4420 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4421 sec, value, &old_bfd, &dynsym))
4422 goto error_free_vers;
4424 /* Check the alignment when a common symbol is involved. This
4425 can change when a common symbol is overridden by a normal
4426 definition or a common symbol is ignored due to the old
4427 normal definition. We need to make sure the maximum
4428 alignment is maintained. */
4429 if ((old_alignment || common)
4430 && h->root.type != bfd_link_hash_common)
4432 unsigned int common_align;
4433 unsigned int normal_align;
4434 unsigned int symbol_align;
4438 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4439 || h->root.type == bfd_link_hash_defweak);
4441 symbol_align = ffs (h->root.u.def.value) - 1;
4442 if (h->root.u.def.section->owner != NULL
4443 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4445 normal_align = h->root.u.def.section->alignment_power;
4446 if (normal_align > symbol_align)
4447 normal_align = symbol_align;
4450 normal_align = symbol_align;
4454 common_align = old_alignment;
4455 common_bfd = old_bfd;
4460 common_align = bfd_log2 (isym->st_value);
4462 normal_bfd = old_bfd;
4465 if (normal_align < common_align)
4467 /* PR binutils/2735 */
4468 if (normal_bfd == NULL)
4469 (*_bfd_error_handler)
4470 (_("Warning: alignment %u of common symbol `%s' in %B is"
4471 " greater than the alignment (%u) of its section %A"),
4472 common_bfd, h->root.u.def.section,
4473 1 << common_align, name, 1 << normal_align);
4475 (*_bfd_error_handler)
4476 (_("Warning: alignment %u of symbol `%s' in %B"
4477 " is smaller than %u in %B"),
4478 normal_bfd, common_bfd,
4479 1 << normal_align, name, 1 << common_align);
4483 /* Remember the symbol size if it isn't undefined. */
4484 if (isym->st_size != 0
4485 && isym->st_shndx != SHN_UNDEF
4486 && (definition || h->size == 0))
4489 && h->size != isym->st_size
4490 && ! size_change_ok)
4491 (*_bfd_error_handler)
4492 (_("Warning: size of symbol `%s' changed"
4493 " from %lu in %B to %lu in %B"),
4495 name, (unsigned long) h->size,
4496 (unsigned long) isym->st_size);
4498 h->size = isym->st_size;
4501 /* If this is a common symbol, then we always want H->SIZE
4502 to be the size of the common symbol. The code just above
4503 won't fix the size if a common symbol becomes larger. We
4504 don't warn about a size change here, because that is
4505 covered by --warn-common. Allow changes between different
4507 if (h->root.type == bfd_link_hash_common)
4508 h->size = h->root.u.c.size;
4510 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4511 && ((definition && !new_weak)
4512 || (old_weak && h->root.type == bfd_link_hash_common)
4513 || h->type == STT_NOTYPE))
4515 unsigned int type = ELF_ST_TYPE (isym->st_info);
4517 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4519 if (type == STT_GNU_IFUNC
4520 && (abfd->flags & DYNAMIC) != 0)
4523 if (h->type != type)
4525 if (h->type != STT_NOTYPE && ! type_change_ok)
4526 (*_bfd_error_handler)
4527 (_("Warning: type of symbol `%s' changed"
4528 " from %d to %d in %B"),
4529 abfd, name, h->type, type);
4535 /* Merge st_other field. */
4536 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4538 /* We don't want to make debug symbol dynamic. */
4540 && (sec->flags & SEC_DEBUGGING)
4541 && !bfd_link_relocatable (info))
4544 /* Nor should we make plugin symbols dynamic. */
4545 if ((abfd->flags & BFD_PLUGIN) != 0)
4550 h->target_internal = isym->st_target_internal;
4551 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4554 if (definition && !dynamic)
4556 char *p = strchr (name, ELF_VER_CHR);
4557 if (p != NULL && p[1] != ELF_VER_CHR)
4559 /* Queue non-default versions so that .symver x, x@FOO
4560 aliases can be checked. */
4563 amt = ((isymend - isym + 1)
4564 * sizeof (struct elf_link_hash_entry *));
4566 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4568 goto error_free_vers;
4570 nondeflt_vers[nondeflt_vers_cnt++] = h;
4574 if (dynsym && h->dynindx == -1)
4576 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4577 goto error_free_vers;
4578 if (h->u.weakdef != NULL
4580 && h->u.weakdef->dynindx == -1)
4582 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4583 goto error_free_vers;
4586 else if (h->dynindx != -1)
4587 /* If the symbol already has a dynamic index, but
4588 visibility says it should not be visible, turn it into
4590 switch (ELF_ST_VISIBILITY (h->other))
4594 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4599 /* Don't add DT_NEEDED for references from the dummy bfd nor
4600 for unmatched symbol. */
4605 && h->ref_regular_nonweak
4607 || (old_bfd->flags & BFD_PLUGIN) == 0))
4608 || (h->ref_dynamic_nonweak
4609 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4610 && !on_needed_list (elf_dt_name (abfd),
4611 htab->needed, NULL))))
4614 const char *soname = elf_dt_name (abfd);
4616 info->callbacks->minfo ("%!", soname, old_bfd,
4617 h->root.root.string);
4619 /* A symbol from a library loaded via DT_NEEDED of some
4620 other library is referenced by a regular object.
4621 Add a DT_NEEDED entry for it. Issue an error if
4622 --no-add-needed is used and the reference was not
4625 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4627 (*_bfd_error_handler)
4628 (_("%B: undefined reference to symbol '%s'"),
4630 bfd_set_error (bfd_error_missing_dso);
4631 goto error_free_vers;
4634 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4635 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4638 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4640 goto error_free_vers;
4642 BFD_ASSERT (ret == 0);
4647 if (extversym != NULL)
4653 if (isymbuf != NULL)
4659 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4663 /* Restore the symbol table. */
4664 old_ent = (char *) old_tab + tabsize;
4665 memset (elf_sym_hashes (abfd), 0,
4666 extsymcount * sizeof (struct elf_link_hash_entry *));
4667 htab->root.table.table = old_table;
4668 htab->root.table.size = old_size;
4669 htab->root.table.count = old_count;
4670 memcpy (htab->root.table.table, old_tab, tabsize);
4671 htab->root.undefs = old_undefs;
4672 htab->root.undefs_tail = old_undefs_tail;
4673 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4674 for (i = 0; i < htab->root.table.size; i++)
4676 struct bfd_hash_entry *p;
4677 struct elf_link_hash_entry *h;
4679 unsigned int alignment_power;
4681 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4683 h = (struct elf_link_hash_entry *) p;
4684 if (h->root.type == bfd_link_hash_warning)
4685 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4686 if (h->dynindx >= old_dynsymcount
4687 && h->dynstr_index < old_dynstr_size)
4688 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4690 /* Preserve the maximum alignment and size for common
4691 symbols even if this dynamic lib isn't on DT_NEEDED
4692 since it can still be loaded at run time by another
4694 if (h->root.type == bfd_link_hash_common)
4696 size = h->root.u.c.size;
4697 alignment_power = h->root.u.c.p->alignment_power;
4702 alignment_power = 0;
4704 memcpy (p, old_ent, htab->root.table.entsize);
4705 old_ent = (char *) old_ent + htab->root.table.entsize;
4706 h = (struct elf_link_hash_entry *) p;
4707 if (h->root.type == bfd_link_hash_warning)
4709 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4710 old_ent = (char *) old_ent + htab->root.table.entsize;
4711 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4713 if (h->root.type == bfd_link_hash_common)
4715 if (size > h->root.u.c.size)
4716 h->root.u.c.size = size;
4717 if (alignment_power > h->root.u.c.p->alignment_power)
4718 h->root.u.c.p->alignment_power = alignment_power;
4723 /* Make a special call to the linker "notice" function to
4724 tell it that symbols added for crefs may need to be removed. */
4725 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4726 goto error_free_vers;
4729 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4731 if (nondeflt_vers != NULL)
4732 free (nondeflt_vers);
4736 if (old_tab != NULL)
4738 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4739 goto error_free_vers;
4744 /* Now that all the symbols from this input file are created, if
4745 not performing a relocatable link, handle .symver foo, foo@BAR
4746 such that any relocs against foo become foo@BAR. */
4747 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4749 bfd_size_type cnt, symidx;
4751 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4753 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4754 char *shortname, *p;
4756 p = strchr (h->root.root.string, ELF_VER_CHR);
4758 || (h->root.type != bfd_link_hash_defined
4759 && h->root.type != bfd_link_hash_defweak))
4762 amt = p - h->root.root.string;
4763 shortname = (char *) bfd_malloc (amt + 1);
4765 goto error_free_vers;
4766 memcpy (shortname, h->root.root.string, amt);
4767 shortname[amt] = '\0';
4769 hi = (struct elf_link_hash_entry *)
4770 bfd_link_hash_lookup (&htab->root, shortname,
4771 FALSE, FALSE, FALSE);
4773 && hi->root.type == h->root.type
4774 && hi->root.u.def.value == h->root.u.def.value
4775 && hi->root.u.def.section == h->root.u.def.section)
4777 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4778 hi->root.type = bfd_link_hash_indirect;
4779 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4780 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4781 sym_hash = elf_sym_hashes (abfd);
4783 for (symidx = 0; symidx < extsymcount; ++symidx)
4784 if (sym_hash[symidx] == hi)
4786 sym_hash[symidx] = h;
4792 free (nondeflt_vers);
4793 nondeflt_vers = NULL;
4796 /* Now set the weakdefs field correctly for all the weak defined
4797 symbols we found. The only way to do this is to search all the
4798 symbols. Since we only need the information for non functions in
4799 dynamic objects, that's the only time we actually put anything on
4800 the list WEAKS. We need this information so that if a regular
4801 object refers to a symbol defined weakly in a dynamic object, the
4802 real symbol in the dynamic object is also put in the dynamic
4803 symbols; we also must arrange for both symbols to point to the
4804 same memory location. We could handle the general case of symbol
4805 aliasing, but a general symbol alias can only be generated in
4806 assembler code, handling it correctly would be very time
4807 consuming, and other ELF linkers don't handle general aliasing
4811 struct elf_link_hash_entry **hpp;
4812 struct elf_link_hash_entry **hppend;
4813 struct elf_link_hash_entry **sorted_sym_hash;
4814 struct elf_link_hash_entry *h;
4817 /* Since we have to search the whole symbol list for each weak
4818 defined symbol, search time for N weak defined symbols will be
4819 O(N^2). Binary search will cut it down to O(NlogN). */
4820 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4821 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4822 if (sorted_sym_hash == NULL)
4824 sym_hash = sorted_sym_hash;
4825 hpp = elf_sym_hashes (abfd);
4826 hppend = hpp + extsymcount;
4828 for (; hpp < hppend; hpp++)
4832 && h->root.type == bfd_link_hash_defined
4833 && !bed->is_function_type (h->type))
4841 qsort (sorted_sym_hash, sym_count,
4842 sizeof (struct elf_link_hash_entry *),
4845 while (weaks != NULL)
4847 struct elf_link_hash_entry *hlook;
4850 size_t i, j, idx = 0;
4853 weaks = hlook->u.weakdef;
4854 hlook->u.weakdef = NULL;
4856 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4857 || hlook->root.type == bfd_link_hash_defweak
4858 || hlook->root.type == bfd_link_hash_common
4859 || hlook->root.type == bfd_link_hash_indirect);
4860 slook = hlook->root.u.def.section;
4861 vlook = hlook->root.u.def.value;
4867 bfd_signed_vma vdiff;
4869 h = sorted_sym_hash[idx];
4870 vdiff = vlook - h->root.u.def.value;
4877 int sdiff = slook->id - h->root.u.def.section->id;
4887 /* We didn't find a value/section match. */
4891 /* With multiple aliases, or when the weak symbol is already
4892 strongly defined, we have multiple matching symbols and
4893 the binary search above may land on any of them. Step
4894 one past the matching symbol(s). */
4897 h = sorted_sym_hash[idx];
4898 if (h->root.u.def.section != slook
4899 || h->root.u.def.value != vlook)
4903 /* Now look back over the aliases. Since we sorted by size
4904 as well as value and section, we'll choose the one with
4905 the largest size. */
4908 h = sorted_sym_hash[idx];
4910 /* Stop if value or section doesn't match. */
4911 if (h->root.u.def.section != slook
4912 || h->root.u.def.value != vlook)
4914 else if (h != hlook)
4916 hlook->u.weakdef = h;
4918 /* If the weak definition is in the list of dynamic
4919 symbols, make sure the real definition is put
4921 if (hlook->dynindx != -1 && h->dynindx == -1)
4923 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4926 free (sorted_sym_hash);
4931 /* If the real definition is in the list of dynamic
4932 symbols, make sure the weak definition is put
4933 there as well. If we don't do this, then the
4934 dynamic loader might not merge the entries for the
4935 real definition and the weak definition. */
4936 if (h->dynindx != -1 && hlook->dynindx == -1)
4938 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4939 goto err_free_sym_hash;
4946 free (sorted_sym_hash);
4949 if (bed->check_directives
4950 && !(*bed->check_directives) (abfd, info))
4953 /* If this object is the same format as the output object, and it is
4954 not a shared library, then let the backend look through the
4957 This is required to build global offset table entries and to
4958 arrange for dynamic relocs. It is not required for the
4959 particular common case of linking non PIC code, even when linking
4960 against shared libraries, but unfortunately there is no way of
4961 knowing whether an object file has been compiled PIC or not.
4962 Looking through the relocs is not particularly time consuming.
4963 The problem is that we must either (1) keep the relocs in memory,
4964 which causes the linker to require additional runtime memory or
4965 (2) read the relocs twice from the input file, which wastes time.
4966 This would be a good case for using mmap.
4968 I have no idea how to handle linking PIC code into a file of a
4969 different format. It probably can't be done. */
4971 && is_elf_hash_table (htab)
4972 && bed->check_relocs != NULL
4973 && elf_object_id (abfd) == elf_hash_table_id (htab)
4974 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4978 for (o = abfd->sections; o != NULL; o = o->next)
4980 Elf_Internal_Rela *internal_relocs;
4983 if ((o->flags & SEC_RELOC) == 0
4984 || o->reloc_count == 0
4985 || ((info->strip == strip_all || info->strip == strip_debugger)
4986 && (o->flags & SEC_DEBUGGING) != 0)
4987 || bfd_is_abs_section (o->output_section))
4990 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4992 if (internal_relocs == NULL)
4995 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4997 if (elf_section_data (o)->relocs != internal_relocs)
4998 free (internal_relocs);
5005 /* If this is a non-traditional link, try to optimize the handling
5006 of the .stab/.stabstr sections. */
5008 && ! info->traditional_format
5009 && is_elf_hash_table (htab)
5010 && (info->strip != strip_all && info->strip != strip_debugger))
5014 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5015 if (stabstr != NULL)
5017 bfd_size_type string_offset = 0;
5020 for (stab = abfd->sections; stab; stab = stab->next)
5021 if (CONST_STRNEQ (stab->name, ".stab")
5022 && (!stab->name[5] ||
5023 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5024 && (stab->flags & SEC_MERGE) == 0
5025 && !bfd_is_abs_section (stab->output_section))
5027 struct bfd_elf_section_data *secdata;
5029 secdata = elf_section_data (stab);
5030 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5031 stabstr, &secdata->sec_info,
5034 if (secdata->sec_info)
5035 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5040 if (is_elf_hash_table (htab) && add_needed)
5042 /* Add this bfd to the loaded list. */
5043 struct elf_link_loaded_list *n;
5045 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5049 n->next = htab->loaded;
5056 if (old_tab != NULL)
5058 if (nondeflt_vers != NULL)
5059 free (nondeflt_vers);
5060 if (extversym != NULL)
5063 if (isymbuf != NULL)
5069 /* Return the linker hash table entry of a symbol that might be
5070 satisfied by an archive symbol. Return -1 on error. */
5072 struct elf_link_hash_entry *
5073 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5074 struct bfd_link_info *info,
5077 struct elf_link_hash_entry *h;
5081 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5085 /* If this is a default version (the name contains @@), look up the
5086 symbol again with only one `@' as well as without the version.
5087 The effect is that references to the symbol with and without the
5088 version will be matched by the default symbol in the archive. */
5090 p = strchr (name, ELF_VER_CHR);
5091 if (p == NULL || p[1] != ELF_VER_CHR)
5094 /* First check with only one `@'. */
5095 len = strlen (name);
5096 copy = (char *) bfd_alloc (abfd, len);
5098 return (struct elf_link_hash_entry *) 0 - 1;
5100 first = p - name + 1;
5101 memcpy (copy, name, first);
5102 memcpy (copy + first, name + first + 1, len - first);
5104 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5107 /* We also need to check references to the symbol without the
5109 copy[first - 1] = '\0';
5110 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5111 FALSE, FALSE, TRUE);
5114 bfd_release (abfd, copy);
5118 /* Add symbols from an ELF archive file to the linker hash table. We
5119 don't use _bfd_generic_link_add_archive_symbols because we need to
5120 handle versioned symbols.
5122 Fortunately, ELF archive handling is simpler than that done by
5123 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5124 oddities. In ELF, if we find a symbol in the archive map, and the
5125 symbol is currently undefined, we know that we must pull in that
5128 Unfortunately, we do have to make multiple passes over the symbol
5129 table until nothing further is resolved. */
5132 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5135 unsigned char *included = NULL;
5139 const struct elf_backend_data *bed;
5140 struct elf_link_hash_entry * (*archive_symbol_lookup)
5141 (bfd *, struct bfd_link_info *, const char *);
5143 if (! bfd_has_map (abfd))
5145 /* An empty archive is a special case. */
5146 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5148 bfd_set_error (bfd_error_no_armap);
5152 /* Keep track of all symbols we know to be already defined, and all
5153 files we know to be already included. This is to speed up the
5154 second and subsequent passes. */
5155 c = bfd_ardata (abfd)->symdef_count;
5159 amt *= sizeof (*included);
5160 included = (unsigned char *) bfd_zmalloc (amt);
5161 if (included == NULL)
5164 symdefs = bfd_ardata (abfd)->symdefs;
5165 bed = get_elf_backend_data (abfd);
5166 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5179 symdefend = symdef + c;
5180 for (i = 0; symdef < symdefend; symdef++, i++)
5182 struct elf_link_hash_entry *h;
5184 struct bfd_link_hash_entry *undefs_tail;
5189 if (symdef->file_offset == last)
5195 h = archive_symbol_lookup (abfd, info, symdef->name);
5196 if (h == (struct elf_link_hash_entry *) 0 - 1)
5202 if (h->root.type == bfd_link_hash_common)
5204 /* We currently have a common symbol. The archive map contains
5205 a reference to this symbol, so we may want to include it. We
5206 only want to include it however, if this archive element
5207 contains a definition of the symbol, not just another common
5210 Unfortunately some archivers (including GNU ar) will put
5211 declarations of common symbols into their archive maps, as
5212 well as real definitions, so we cannot just go by the archive
5213 map alone. Instead we must read in the element's symbol
5214 table and check that to see what kind of symbol definition
5216 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5219 else if (h->root.type != bfd_link_hash_undefined)
5221 if (h->root.type != bfd_link_hash_undefweak)
5222 /* Symbol must be defined. Don't check it again. */
5227 /* We need to include this archive member. */
5228 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5229 if (element == NULL)
5232 if (! bfd_check_format (element, bfd_object))
5235 undefs_tail = info->hash->undefs_tail;
5237 if (!(*info->callbacks
5238 ->add_archive_element) (info, element, symdef->name, &element))
5240 if (!bfd_link_add_symbols (element, info))
5243 /* If there are any new undefined symbols, we need to make
5244 another pass through the archive in order to see whether
5245 they can be defined. FIXME: This isn't perfect, because
5246 common symbols wind up on undefs_tail and because an
5247 undefined symbol which is defined later on in this pass
5248 does not require another pass. This isn't a bug, but it
5249 does make the code less efficient than it could be. */
5250 if (undefs_tail != info->hash->undefs_tail)
5253 /* Look backward to mark all symbols from this object file
5254 which we have already seen in this pass. */
5258 included[mark] = TRUE;
5263 while (symdefs[mark].file_offset == symdef->file_offset);
5265 /* We mark subsequent symbols from this object file as we go
5266 on through the loop. */
5267 last = symdef->file_offset;
5277 if (included != NULL)
5282 /* Given an ELF BFD, add symbols to the global hash table as
5286 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5288 switch (bfd_get_format (abfd))
5291 return elf_link_add_object_symbols (abfd, info);
5293 return elf_link_add_archive_symbols (abfd, info);
5295 bfd_set_error (bfd_error_wrong_format);
5300 struct hash_codes_info
5302 unsigned long *hashcodes;
5306 /* This function will be called though elf_link_hash_traverse to store
5307 all hash value of the exported symbols in an array. */
5310 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5312 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5317 /* Ignore indirect symbols. These are added by the versioning code. */
5318 if (h->dynindx == -1)
5321 name = h->root.root.string;
5322 if (h->versioned >= versioned)
5324 char *p = strchr (name, ELF_VER_CHR);
5327 alc = (char *) bfd_malloc (p - name + 1);
5333 memcpy (alc, name, p - name);
5334 alc[p - name] = '\0';
5339 /* Compute the hash value. */
5340 ha = bfd_elf_hash (name);
5342 /* Store the found hash value in the array given as the argument. */
5343 *(inf->hashcodes)++ = ha;
5345 /* And store it in the struct so that we can put it in the hash table
5347 h->u.elf_hash_value = ha;
5355 struct collect_gnu_hash_codes
5358 const struct elf_backend_data *bed;
5359 unsigned long int nsyms;
5360 unsigned long int maskbits;
5361 unsigned long int *hashcodes;
5362 unsigned long int *hashval;
5363 unsigned long int *indx;
5364 unsigned long int *counts;
5367 long int min_dynindx;
5368 unsigned long int bucketcount;
5369 unsigned long int symindx;
5370 long int local_indx;
5371 long int shift1, shift2;
5372 unsigned long int mask;
5376 /* This function will be called though elf_link_hash_traverse to store
5377 all hash value of the exported symbols in an array. */
5380 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5382 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5387 /* Ignore indirect symbols. These are added by the versioning code. */
5388 if (h->dynindx == -1)
5391 /* Ignore also local symbols and undefined symbols. */
5392 if (! (*s->bed->elf_hash_symbol) (h))
5395 name = h->root.root.string;
5396 if (h->versioned >= versioned)
5398 char *p = strchr (name, ELF_VER_CHR);
5401 alc = (char *) bfd_malloc (p - name + 1);
5407 memcpy (alc, name, p - name);
5408 alc[p - name] = '\0';
5413 /* Compute the hash value. */
5414 ha = bfd_elf_gnu_hash (name);
5416 /* Store the found hash value in the array for compute_bucket_count,
5417 and also for .dynsym reordering purposes. */
5418 s->hashcodes[s->nsyms] = ha;
5419 s->hashval[h->dynindx] = ha;
5421 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5422 s->min_dynindx = h->dynindx;
5430 /* This function will be called though elf_link_hash_traverse to do
5431 final dynaminc symbol renumbering. */
5434 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5436 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5437 unsigned long int bucket;
5438 unsigned long int val;
5440 /* Ignore indirect symbols. */
5441 if (h->dynindx == -1)
5444 /* Ignore also local symbols and undefined symbols. */
5445 if (! (*s->bed->elf_hash_symbol) (h))
5447 if (h->dynindx >= s->min_dynindx)
5448 h->dynindx = s->local_indx++;
5452 bucket = s->hashval[h->dynindx] % s->bucketcount;
5453 val = (s->hashval[h->dynindx] >> s->shift1)
5454 & ((s->maskbits >> s->shift1) - 1);
5455 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5457 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5458 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5459 if (s->counts[bucket] == 1)
5460 /* Last element terminates the chain. */
5462 bfd_put_32 (s->output_bfd, val,
5463 s->contents + (s->indx[bucket] - s->symindx) * 4);
5464 --s->counts[bucket];
5465 h->dynindx = s->indx[bucket]++;
5469 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5472 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5474 return !(h->forced_local
5475 || h->root.type == bfd_link_hash_undefined
5476 || h->root.type == bfd_link_hash_undefweak
5477 || ((h->root.type == bfd_link_hash_defined
5478 || h->root.type == bfd_link_hash_defweak)
5479 && h->root.u.def.section->output_section == NULL));
5482 /* Array used to determine the number of hash table buckets to use
5483 based on the number of symbols there are. If there are fewer than
5484 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5485 fewer than 37 we use 17 buckets, and so forth. We never use more
5486 than 32771 buckets. */
5488 static const size_t elf_buckets[] =
5490 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5494 /* Compute bucket count for hashing table. We do not use a static set
5495 of possible tables sizes anymore. Instead we determine for all
5496 possible reasonable sizes of the table the outcome (i.e., the
5497 number of collisions etc) and choose the best solution. The
5498 weighting functions are not too simple to allow the table to grow
5499 without bounds. Instead one of the weighting factors is the size.
5500 Therefore the result is always a good payoff between few collisions
5501 (= short chain lengths) and table size. */
5503 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5504 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5505 unsigned long int nsyms,
5508 size_t best_size = 0;
5509 unsigned long int i;
5511 /* We have a problem here. The following code to optimize the table
5512 size requires an integer type with more the 32 bits. If
5513 BFD_HOST_U_64_BIT is set we know about such a type. */
5514 #ifdef BFD_HOST_U_64_BIT
5519 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5520 bfd *dynobj = elf_hash_table (info)->dynobj;
5521 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5522 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5523 unsigned long int *counts;
5525 unsigned int no_improvement_count = 0;
5527 /* Possible optimization parameters: if we have NSYMS symbols we say
5528 that the hashing table must at least have NSYMS/4 and at most
5530 minsize = nsyms / 4;
5533 best_size = maxsize = nsyms * 2;
5538 if ((best_size & 31) == 0)
5542 /* Create array where we count the collisions in. We must use bfd_malloc
5543 since the size could be large. */
5545 amt *= sizeof (unsigned long int);
5546 counts = (unsigned long int *) bfd_malloc (amt);
5550 /* Compute the "optimal" size for the hash table. The criteria is a
5551 minimal chain length. The minor criteria is (of course) the size
5553 for (i = minsize; i < maxsize; ++i)
5555 /* Walk through the array of hashcodes and count the collisions. */
5556 BFD_HOST_U_64_BIT max;
5557 unsigned long int j;
5558 unsigned long int fact;
5560 if (gnu_hash && (i & 31) == 0)
5563 memset (counts, '\0', i * sizeof (unsigned long int));
5565 /* Determine how often each hash bucket is used. */
5566 for (j = 0; j < nsyms; ++j)
5567 ++counts[hashcodes[j] % i];
5569 /* For the weight function we need some information about the
5570 pagesize on the target. This is information need not be 100%
5571 accurate. Since this information is not available (so far) we
5572 define it here to a reasonable default value. If it is crucial
5573 to have a better value some day simply define this value. */
5574 # ifndef BFD_TARGET_PAGESIZE
5575 # define BFD_TARGET_PAGESIZE (4096)
5578 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5580 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5583 /* Variant 1: optimize for short chains. We add the squares
5584 of all the chain lengths (which favors many small chain
5585 over a few long chains). */
5586 for (j = 0; j < i; ++j)
5587 max += counts[j] * counts[j];
5589 /* This adds penalties for the overall size of the table. */
5590 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5593 /* Variant 2: Optimize a lot more for small table. Here we
5594 also add squares of the size but we also add penalties for
5595 empty slots (the +1 term). */
5596 for (j = 0; j < i; ++j)
5597 max += (1 + counts[j]) * (1 + counts[j]);
5599 /* The overall size of the table is considered, but not as
5600 strong as in variant 1, where it is squared. */
5601 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5605 /* Compare with current best results. */
5606 if (max < best_chlen)
5610 no_improvement_count = 0;
5612 /* PR 11843: Avoid futile long searches for the best bucket size
5613 when there are a large number of symbols. */
5614 else if (++no_improvement_count == 100)
5621 #endif /* defined (BFD_HOST_U_64_BIT) */
5623 /* This is the fallback solution if no 64bit type is available or if we
5624 are not supposed to spend much time on optimizations. We select the
5625 bucket count using a fixed set of numbers. */
5626 for (i = 0; elf_buckets[i] != 0; i++)
5628 best_size = elf_buckets[i];
5629 if (nsyms < elf_buckets[i + 1])
5632 if (gnu_hash && best_size < 2)
5639 /* Size any SHT_GROUP section for ld -r. */
5642 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5646 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5647 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5648 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5653 /* Set a default stack segment size. The value in INFO wins. If it
5654 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5655 undefined it is initialized. */
5658 bfd_elf_stack_segment_size (bfd *output_bfd,
5659 struct bfd_link_info *info,
5660 const char *legacy_symbol,
5661 bfd_vma default_size)
5663 struct elf_link_hash_entry *h = NULL;
5665 /* Look for legacy symbol. */
5667 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5668 FALSE, FALSE, FALSE);
5669 if (h && (h->root.type == bfd_link_hash_defined
5670 || h->root.type == bfd_link_hash_defweak)
5672 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5674 /* The symbol has no type if specified on the command line. */
5675 h->type = STT_OBJECT;
5676 if (info->stacksize)
5677 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5678 output_bfd, legacy_symbol);
5679 else if (h->root.u.def.section != bfd_abs_section_ptr)
5680 (*_bfd_error_handler) (_("%B: %s not absolute"),
5681 output_bfd, legacy_symbol);
5683 info->stacksize = h->root.u.def.value;
5686 if (!info->stacksize)
5687 /* If the user didn't set a size, or explicitly inhibit the
5688 size, set it now. */
5689 info->stacksize = default_size;
5691 /* Provide the legacy symbol, if it is referenced. */
5692 if (h && (h->root.type == bfd_link_hash_undefined
5693 || h->root.type == bfd_link_hash_undefweak))
5695 struct bfd_link_hash_entry *bh = NULL;
5697 if (!(_bfd_generic_link_add_one_symbol
5698 (info, output_bfd, legacy_symbol,
5699 BSF_GLOBAL, bfd_abs_section_ptr,
5700 info->stacksize >= 0 ? info->stacksize : 0,
5701 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5704 h = (struct elf_link_hash_entry *) bh;
5706 h->type = STT_OBJECT;
5712 /* Set up the sizes and contents of the ELF dynamic sections. This is
5713 called by the ELF linker emulation before_allocation routine. We
5714 must set the sizes of the sections before the linker sets the
5715 addresses of the various sections. */
5718 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5721 const char *filter_shlib,
5723 const char *depaudit,
5724 const char * const *auxiliary_filters,
5725 struct bfd_link_info *info,
5726 asection **sinterpptr)
5728 bfd_size_type soname_indx;
5730 const struct elf_backend_data *bed;
5731 struct elf_info_failed asvinfo;
5735 soname_indx = (bfd_size_type) -1;
5737 if (!is_elf_hash_table (info->hash))
5740 bed = get_elf_backend_data (output_bfd);
5742 /* Any syms created from now on start with -1 in
5743 got.refcount/offset and plt.refcount/offset. */
5744 elf_hash_table (info)->init_got_refcount
5745 = elf_hash_table (info)->init_got_offset;
5746 elf_hash_table (info)->init_plt_refcount
5747 = elf_hash_table (info)->init_plt_offset;
5749 if (bfd_link_relocatable (info)
5750 && !_bfd_elf_size_group_sections (info))
5753 /* The backend may have to create some sections regardless of whether
5754 we're dynamic or not. */
5755 if (bed->elf_backend_always_size_sections
5756 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5759 /* Determine any GNU_STACK segment requirements, after the backend
5760 has had a chance to set a default segment size. */
5761 if (info->execstack)
5762 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5763 else if (info->noexecstack)
5764 elf_stack_flags (output_bfd) = PF_R | PF_W;
5768 asection *notesec = NULL;
5771 for (inputobj = info->input_bfds;
5773 inputobj = inputobj->link.next)
5778 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5780 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5783 if (s->flags & SEC_CODE)
5787 else if (bed->default_execstack)
5790 if (notesec || info->stacksize > 0)
5791 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5792 if (notesec && exec && bfd_link_relocatable (info)
5793 && notesec->output_section != bfd_abs_section_ptr)
5794 notesec->output_section->flags |= SEC_CODE;
5797 dynobj = elf_hash_table (info)->dynobj;
5799 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5801 struct elf_info_failed eif;
5802 struct elf_link_hash_entry *h;
5804 struct bfd_elf_version_tree *t;
5805 struct bfd_elf_version_expr *d;
5807 bfd_boolean all_defined;
5809 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5810 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5814 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5816 if (soname_indx == (bfd_size_type) -1
5817 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5823 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5825 info->flags |= DF_SYMBOLIC;
5833 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5835 if (indx == (bfd_size_type) -1)
5838 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5839 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5843 if (filter_shlib != NULL)
5847 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5848 filter_shlib, TRUE);
5849 if (indx == (bfd_size_type) -1
5850 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5854 if (auxiliary_filters != NULL)
5856 const char * const *p;
5858 for (p = auxiliary_filters; *p != NULL; p++)
5862 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5864 if (indx == (bfd_size_type) -1
5865 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5874 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5876 if (indx == (bfd_size_type) -1
5877 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5881 if (depaudit != NULL)
5885 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5887 if (indx == (bfd_size_type) -1
5888 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5895 /* If we are supposed to export all symbols into the dynamic symbol
5896 table (this is not the normal case), then do so. */
5897 if (info->export_dynamic
5898 || (bfd_link_executable (info) && info->dynamic))
5900 elf_link_hash_traverse (elf_hash_table (info),
5901 _bfd_elf_export_symbol,
5907 /* Make all global versions with definition. */
5908 for (t = info->version_info; t != NULL; t = t->next)
5909 for (d = t->globals.list; d != NULL; d = d->next)
5910 if (!d->symver && d->literal)
5912 const char *verstr, *name;
5913 size_t namelen, verlen, newlen;
5914 char *newname, *p, leading_char;
5915 struct elf_link_hash_entry *newh;
5917 leading_char = bfd_get_symbol_leading_char (output_bfd);
5919 namelen = strlen (name) + (leading_char != '\0');
5921 verlen = strlen (verstr);
5922 newlen = namelen + verlen + 3;
5924 newname = (char *) bfd_malloc (newlen);
5925 if (newname == NULL)
5927 newname[0] = leading_char;
5928 memcpy (newname + (leading_char != '\0'), name, namelen);
5930 /* Check the hidden versioned definition. */
5931 p = newname + namelen;
5933 memcpy (p, verstr, verlen + 1);
5934 newh = elf_link_hash_lookup (elf_hash_table (info),
5935 newname, FALSE, FALSE,
5938 || (newh->root.type != bfd_link_hash_defined
5939 && newh->root.type != bfd_link_hash_defweak))
5941 /* Check the default versioned definition. */
5943 memcpy (p, verstr, verlen + 1);
5944 newh = elf_link_hash_lookup (elf_hash_table (info),
5945 newname, FALSE, FALSE,
5950 /* Mark this version if there is a definition and it is
5951 not defined in a shared object. */
5953 && !newh->def_dynamic
5954 && (newh->root.type == bfd_link_hash_defined
5955 || newh->root.type == bfd_link_hash_defweak))
5959 /* Attach all the symbols to their version information. */
5960 asvinfo.info = info;
5961 asvinfo.failed = FALSE;
5963 elf_link_hash_traverse (elf_hash_table (info),
5964 _bfd_elf_link_assign_sym_version,
5969 if (!info->allow_undefined_version)
5971 /* Check if all global versions have a definition. */
5973 for (t = info->version_info; t != NULL; t = t->next)
5974 for (d = t->globals.list; d != NULL; d = d->next)
5975 if (d->literal && !d->symver && !d->script)
5977 (*_bfd_error_handler)
5978 (_("%s: undefined version: %s"),
5979 d->pattern, t->name);
5980 all_defined = FALSE;
5985 bfd_set_error (bfd_error_bad_value);
5990 /* Find all symbols which were defined in a dynamic object and make
5991 the backend pick a reasonable value for them. */
5992 elf_link_hash_traverse (elf_hash_table (info),
5993 _bfd_elf_adjust_dynamic_symbol,
5998 /* Add some entries to the .dynamic section. We fill in some of the
5999 values later, in bfd_elf_final_link, but we must add the entries
6000 now so that we know the final size of the .dynamic section. */
6002 /* If there are initialization and/or finalization functions to
6003 call then add the corresponding DT_INIT/DT_FINI entries. */
6004 h = (info->init_function
6005 ? elf_link_hash_lookup (elf_hash_table (info),
6006 info->init_function, FALSE,
6013 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6016 h = (info->fini_function
6017 ? elf_link_hash_lookup (elf_hash_table (info),
6018 info->fini_function, FALSE,
6025 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6029 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6030 if (s != NULL && s->linker_has_input)
6032 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6033 if (! bfd_link_executable (info))
6038 for (sub = info->input_bfds; sub != NULL;
6039 sub = sub->link.next)
6040 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6041 for (o = sub->sections; o != NULL; o = o->next)
6042 if (elf_section_data (o)->this_hdr.sh_type
6043 == SHT_PREINIT_ARRAY)
6045 (*_bfd_error_handler)
6046 (_("%B: .preinit_array section is not allowed in DSO"),
6051 bfd_set_error (bfd_error_nonrepresentable_section);
6055 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6056 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6059 s = bfd_get_section_by_name (output_bfd, ".init_array");
6060 if (s != NULL && s->linker_has_input)
6062 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6063 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6066 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6067 if (s != NULL && s->linker_has_input)
6069 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6070 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6074 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6075 /* If .dynstr is excluded from the link, we don't want any of
6076 these tags. Strictly, we should be checking each section
6077 individually; This quick check covers for the case where
6078 someone does a /DISCARD/ : { *(*) }. */
6079 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6081 bfd_size_type strsize;
6083 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6084 if ((info->emit_hash
6085 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6086 || (info->emit_gnu_hash
6087 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6088 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6089 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6090 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6091 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6092 bed->s->sizeof_sym))
6097 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6100 /* The backend must work out the sizes of all the other dynamic
6103 && bed->elf_backend_size_dynamic_sections != NULL
6104 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6107 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6109 unsigned long section_sym_count;
6110 struct bfd_elf_version_tree *verdefs;
6113 /* Set up the version definition section. */
6114 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6115 BFD_ASSERT (s != NULL);
6117 /* We may have created additional version definitions if we are
6118 just linking a regular application. */
6119 verdefs = info->version_info;
6121 /* Skip anonymous version tag. */
6122 if (verdefs != NULL && verdefs->vernum == 0)
6123 verdefs = verdefs->next;
6125 if (verdefs == NULL && !info->create_default_symver)
6126 s->flags |= SEC_EXCLUDE;
6131 struct bfd_elf_version_tree *t;
6133 Elf_Internal_Verdef def;
6134 Elf_Internal_Verdaux defaux;
6135 struct bfd_link_hash_entry *bh;
6136 struct elf_link_hash_entry *h;
6142 /* Make space for the base version. */
6143 size += sizeof (Elf_External_Verdef);
6144 size += sizeof (Elf_External_Verdaux);
6147 /* Make space for the default version. */
6148 if (info->create_default_symver)
6150 size += sizeof (Elf_External_Verdef);
6154 for (t = verdefs; t != NULL; t = t->next)
6156 struct bfd_elf_version_deps *n;
6158 /* Don't emit base version twice. */
6162 size += sizeof (Elf_External_Verdef);
6163 size += sizeof (Elf_External_Verdaux);
6166 for (n = t->deps; n != NULL; n = n->next)
6167 size += sizeof (Elf_External_Verdaux);
6171 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6172 if (s->contents == NULL && s->size != 0)
6175 /* Fill in the version definition section. */
6179 def.vd_version = VER_DEF_CURRENT;
6180 def.vd_flags = VER_FLG_BASE;
6183 if (info->create_default_symver)
6185 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6186 def.vd_next = sizeof (Elf_External_Verdef);
6190 def.vd_aux = sizeof (Elf_External_Verdef);
6191 def.vd_next = (sizeof (Elf_External_Verdef)
6192 + sizeof (Elf_External_Verdaux));
6195 if (soname_indx != (bfd_size_type) -1)
6197 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6199 def.vd_hash = bfd_elf_hash (soname);
6200 defaux.vda_name = soname_indx;
6207 name = lbasename (output_bfd->filename);
6208 def.vd_hash = bfd_elf_hash (name);
6209 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6211 if (indx == (bfd_size_type) -1)
6213 defaux.vda_name = indx;
6215 defaux.vda_next = 0;
6217 _bfd_elf_swap_verdef_out (output_bfd, &def,
6218 (Elf_External_Verdef *) p);
6219 p += sizeof (Elf_External_Verdef);
6220 if (info->create_default_symver)
6222 /* Add a symbol representing this version. */
6224 if (! (_bfd_generic_link_add_one_symbol
6225 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6227 get_elf_backend_data (dynobj)->collect, &bh)))
6229 h = (struct elf_link_hash_entry *) bh;
6232 h->type = STT_OBJECT;
6233 h->verinfo.vertree = NULL;
6235 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6238 /* Create a duplicate of the base version with the same
6239 aux block, but different flags. */
6242 def.vd_aux = sizeof (Elf_External_Verdef);
6244 def.vd_next = (sizeof (Elf_External_Verdef)
6245 + sizeof (Elf_External_Verdaux));
6248 _bfd_elf_swap_verdef_out (output_bfd, &def,
6249 (Elf_External_Verdef *) p);
6250 p += sizeof (Elf_External_Verdef);
6252 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6253 (Elf_External_Verdaux *) p);
6254 p += sizeof (Elf_External_Verdaux);
6256 for (t = verdefs; t != NULL; t = t->next)
6259 struct bfd_elf_version_deps *n;
6261 /* Don't emit the base version twice. */
6266 for (n = t->deps; n != NULL; n = n->next)
6269 /* Add a symbol representing this version. */
6271 if (! (_bfd_generic_link_add_one_symbol
6272 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6274 get_elf_backend_data (dynobj)->collect, &bh)))
6276 h = (struct elf_link_hash_entry *) bh;
6279 h->type = STT_OBJECT;
6280 h->verinfo.vertree = t;
6282 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6285 def.vd_version = VER_DEF_CURRENT;
6287 if (t->globals.list == NULL
6288 && t->locals.list == NULL
6290 def.vd_flags |= VER_FLG_WEAK;
6291 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6292 def.vd_cnt = cdeps + 1;
6293 def.vd_hash = bfd_elf_hash (t->name);
6294 def.vd_aux = sizeof (Elf_External_Verdef);
6297 /* If a basever node is next, it *must* be the last node in
6298 the chain, otherwise Verdef construction breaks. */
6299 if (t->next != NULL && t->next->vernum == 0)
6300 BFD_ASSERT (t->next->next == NULL);
6302 if (t->next != NULL && t->next->vernum != 0)
6303 def.vd_next = (sizeof (Elf_External_Verdef)
6304 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6306 _bfd_elf_swap_verdef_out (output_bfd, &def,
6307 (Elf_External_Verdef *) p);
6308 p += sizeof (Elf_External_Verdef);
6310 defaux.vda_name = h->dynstr_index;
6311 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6313 defaux.vda_next = 0;
6314 if (t->deps != NULL)
6315 defaux.vda_next = sizeof (Elf_External_Verdaux);
6316 t->name_indx = defaux.vda_name;
6318 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6319 (Elf_External_Verdaux *) p);
6320 p += sizeof (Elf_External_Verdaux);
6322 for (n = t->deps; n != NULL; n = n->next)
6324 if (n->version_needed == NULL)
6326 /* This can happen if there was an error in the
6328 defaux.vda_name = 0;
6332 defaux.vda_name = n->version_needed->name_indx;
6333 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6336 if (n->next == NULL)
6337 defaux.vda_next = 0;
6339 defaux.vda_next = sizeof (Elf_External_Verdaux);
6341 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6342 (Elf_External_Verdaux *) p);
6343 p += sizeof (Elf_External_Verdaux);
6347 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6348 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6351 elf_tdata (output_bfd)->cverdefs = cdefs;
6354 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6356 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6359 else if (info->flags & DF_BIND_NOW)
6361 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6367 if (bfd_link_executable (info))
6368 info->flags_1 &= ~ (DF_1_INITFIRST
6371 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6375 /* Work out the size of the version reference section. */
6377 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6378 BFD_ASSERT (s != NULL);
6380 struct elf_find_verdep_info sinfo;
6383 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6384 if (sinfo.vers == 0)
6386 sinfo.failed = FALSE;
6388 elf_link_hash_traverse (elf_hash_table (info),
6389 _bfd_elf_link_find_version_dependencies,
6394 if (elf_tdata (output_bfd)->verref == NULL)
6395 s->flags |= SEC_EXCLUDE;
6398 Elf_Internal_Verneed *t;
6403 /* Build the version dependency section. */
6406 for (t = elf_tdata (output_bfd)->verref;
6410 Elf_Internal_Vernaux *a;
6412 size += sizeof (Elf_External_Verneed);
6414 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6415 size += sizeof (Elf_External_Vernaux);
6419 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6420 if (s->contents == NULL)
6424 for (t = elf_tdata (output_bfd)->verref;
6429 Elf_Internal_Vernaux *a;
6433 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6436 t->vn_version = VER_NEED_CURRENT;
6438 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6439 elf_dt_name (t->vn_bfd) != NULL
6440 ? elf_dt_name (t->vn_bfd)
6441 : lbasename (t->vn_bfd->filename),
6443 if (indx == (bfd_size_type) -1)
6446 t->vn_aux = sizeof (Elf_External_Verneed);
6447 if (t->vn_nextref == NULL)
6450 t->vn_next = (sizeof (Elf_External_Verneed)
6451 + caux * sizeof (Elf_External_Vernaux));
6453 _bfd_elf_swap_verneed_out (output_bfd, t,
6454 (Elf_External_Verneed *) p);
6455 p += sizeof (Elf_External_Verneed);
6457 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6459 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6460 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6461 a->vna_nodename, FALSE);
6462 if (indx == (bfd_size_type) -1)
6465 if (a->vna_nextptr == NULL)
6468 a->vna_next = sizeof (Elf_External_Vernaux);
6470 _bfd_elf_swap_vernaux_out (output_bfd, a,
6471 (Elf_External_Vernaux *) p);
6472 p += sizeof (Elf_External_Vernaux);
6476 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6477 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6480 elf_tdata (output_bfd)->cverrefs = crefs;
6484 if ((elf_tdata (output_bfd)->cverrefs == 0
6485 && elf_tdata (output_bfd)->cverdefs == 0)
6486 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6487 §ion_sym_count) == 0)
6489 s = bfd_get_linker_section (dynobj, ".gnu.version");
6490 s->flags |= SEC_EXCLUDE;
6496 /* Find the first non-excluded output section. We'll use its
6497 section symbol for some emitted relocs. */
6499 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6503 for (s = output_bfd->sections; s != NULL; s = s->next)
6504 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6505 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6507 elf_hash_table (info)->text_index_section = s;
6512 /* Find two non-excluded output sections, one for code, one for data.
6513 We'll use their section symbols for some emitted relocs. */
6515 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6519 /* Data first, since setting text_index_section changes
6520 _bfd_elf_link_omit_section_dynsym. */
6521 for (s = output_bfd->sections; s != NULL; s = s->next)
6522 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6523 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6525 elf_hash_table (info)->data_index_section = s;
6529 for (s = output_bfd->sections; s != NULL; s = s->next)
6530 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6531 == (SEC_ALLOC | SEC_READONLY))
6532 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6534 elf_hash_table (info)->text_index_section = s;
6538 if (elf_hash_table (info)->text_index_section == NULL)
6539 elf_hash_table (info)->text_index_section
6540 = elf_hash_table (info)->data_index_section;
6544 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6546 const struct elf_backend_data *bed;
6548 if (!is_elf_hash_table (info->hash))
6551 bed = get_elf_backend_data (output_bfd);
6552 (*bed->elf_backend_init_index_section) (output_bfd, info);
6554 if (elf_hash_table (info)->dynamic_sections_created)
6558 bfd_size_type dynsymcount;
6559 unsigned long section_sym_count;
6560 unsigned int dtagcount;
6562 dynobj = elf_hash_table (info)->dynobj;
6564 /* Assign dynsym indicies. In a shared library we generate a
6565 section symbol for each output section, which come first.
6566 Next come all of the back-end allocated local dynamic syms,
6567 followed by the rest of the global symbols. */
6569 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6570 §ion_sym_count);
6572 /* Work out the size of the symbol version section. */
6573 s = bfd_get_linker_section (dynobj, ".gnu.version");
6574 BFD_ASSERT (s != NULL);
6575 if (dynsymcount != 0
6576 && (s->flags & SEC_EXCLUDE) == 0)
6578 s->size = dynsymcount * sizeof (Elf_External_Versym);
6579 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6580 if (s->contents == NULL)
6583 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6587 /* Set the size of the .dynsym and .hash sections. We counted
6588 the number of dynamic symbols in elf_link_add_object_symbols.
6589 We will build the contents of .dynsym and .hash when we build
6590 the final symbol table, because until then we do not know the
6591 correct value to give the symbols. We built the .dynstr
6592 section as we went along in elf_link_add_object_symbols. */
6593 s = elf_hash_table (info)->dynsym;
6594 BFD_ASSERT (s != NULL);
6595 s->size = dynsymcount * bed->s->sizeof_sym;
6597 if (dynsymcount != 0)
6599 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6600 if (s->contents == NULL)
6603 /* The first entry in .dynsym is a dummy symbol.
6604 Clear all the section syms, in case we don't output them all. */
6605 ++section_sym_count;
6606 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6609 elf_hash_table (info)->bucketcount = 0;
6611 /* Compute the size of the hashing table. As a side effect this
6612 computes the hash values for all the names we export. */
6613 if (info->emit_hash)
6615 unsigned long int *hashcodes;
6616 struct hash_codes_info hashinf;
6618 unsigned long int nsyms;
6620 size_t hash_entry_size;
6622 /* Compute the hash values for all exported symbols. At the same
6623 time store the values in an array so that we could use them for
6625 amt = dynsymcount * sizeof (unsigned long int);
6626 hashcodes = (unsigned long int *) bfd_malloc (amt);
6627 if (hashcodes == NULL)
6629 hashinf.hashcodes = hashcodes;
6630 hashinf.error = FALSE;
6632 /* Put all hash values in HASHCODES. */
6633 elf_link_hash_traverse (elf_hash_table (info),
6634 elf_collect_hash_codes, &hashinf);
6641 nsyms = hashinf.hashcodes - hashcodes;
6643 = compute_bucket_count (info, hashcodes, nsyms, 0);
6646 if (bucketcount == 0)
6649 elf_hash_table (info)->bucketcount = bucketcount;
6651 s = bfd_get_linker_section (dynobj, ".hash");
6652 BFD_ASSERT (s != NULL);
6653 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6654 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6655 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6656 if (s->contents == NULL)
6659 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6660 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6661 s->contents + hash_entry_size);
6664 if (info->emit_gnu_hash)
6667 unsigned char *contents;
6668 struct collect_gnu_hash_codes cinfo;
6672 memset (&cinfo, 0, sizeof (cinfo));
6674 /* Compute the hash values for all exported symbols. At the same
6675 time store the values in an array so that we could use them for
6677 amt = dynsymcount * 2 * sizeof (unsigned long int);
6678 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6679 if (cinfo.hashcodes == NULL)
6682 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6683 cinfo.min_dynindx = -1;
6684 cinfo.output_bfd = output_bfd;
6687 /* Put all hash values in HASHCODES. */
6688 elf_link_hash_traverse (elf_hash_table (info),
6689 elf_collect_gnu_hash_codes, &cinfo);
6692 free (cinfo.hashcodes);
6697 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6699 if (bucketcount == 0)
6701 free (cinfo.hashcodes);
6705 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6706 BFD_ASSERT (s != NULL);
6708 if (cinfo.nsyms == 0)
6710 /* Empty .gnu.hash section is special. */
6711 BFD_ASSERT (cinfo.min_dynindx == -1);
6712 free (cinfo.hashcodes);
6713 s->size = 5 * 4 + bed->s->arch_size / 8;
6714 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6715 if (contents == NULL)
6717 s->contents = contents;
6718 /* 1 empty bucket. */
6719 bfd_put_32 (output_bfd, 1, contents);
6720 /* SYMIDX above the special symbol 0. */
6721 bfd_put_32 (output_bfd, 1, contents + 4);
6722 /* Just one word for bitmask. */
6723 bfd_put_32 (output_bfd, 1, contents + 8);
6724 /* Only hash fn bloom filter. */
6725 bfd_put_32 (output_bfd, 0, contents + 12);
6726 /* No hashes are valid - empty bitmask. */
6727 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6728 /* No hashes in the only bucket. */
6729 bfd_put_32 (output_bfd, 0,
6730 contents + 16 + bed->s->arch_size / 8);
6734 unsigned long int maskwords, maskbitslog2, x;
6735 BFD_ASSERT (cinfo.min_dynindx != -1);
6739 while ((x >>= 1) != 0)
6741 if (maskbitslog2 < 3)
6743 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6744 maskbitslog2 = maskbitslog2 + 3;
6746 maskbitslog2 = maskbitslog2 + 2;
6747 if (bed->s->arch_size == 64)
6749 if (maskbitslog2 == 5)
6755 cinfo.mask = (1 << cinfo.shift1) - 1;
6756 cinfo.shift2 = maskbitslog2;
6757 cinfo.maskbits = 1 << maskbitslog2;
6758 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6759 amt = bucketcount * sizeof (unsigned long int) * 2;
6760 amt += maskwords * sizeof (bfd_vma);
6761 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6762 if (cinfo.bitmask == NULL)
6764 free (cinfo.hashcodes);
6768 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6769 cinfo.indx = cinfo.counts + bucketcount;
6770 cinfo.symindx = dynsymcount - cinfo.nsyms;
6771 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6773 /* Determine how often each hash bucket is used. */
6774 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6775 for (i = 0; i < cinfo.nsyms; ++i)
6776 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6778 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6779 if (cinfo.counts[i] != 0)
6781 cinfo.indx[i] = cnt;
6782 cnt += cinfo.counts[i];
6784 BFD_ASSERT (cnt == dynsymcount);
6785 cinfo.bucketcount = bucketcount;
6786 cinfo.local_indx = cinfo.min_dynindx;
6788 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6789 s->size += cinfo.maskbits / 8;
6790 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6791 if (contents == NULL)
6793 free (cinfo.bitmask);
6794 free (cinfo.hashcodes);
6798 s->contents = contents;
6799 bfd_put_32 (output_bfd, bucketcount, contents);
6800 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6801 bfd_put_32 (output_bfd, maskwords, contents + 8);
6802 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6803 contents += 16 + cinfo.maskbits / 8;
6805 for (i = 0; i < bucketcount; ++i)
6807 if (cinfo.counts[i] == 0)
6808 bfd_put_32 (output_bfd, 0, contents);
6810 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6814 cinfo.contents = contents;
6816 /* Renumber dynamic symbols, populate .gnu.hash section. */
6817 elf_link_hash_traverse (elf_hash_table (info),
6818 elf_renumber_gnu_hash_syms, &cinfo);
6820 contents = s->contents + 16;
6821 for (i = 0; i < maskwords; ++i)
6823 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6825 contents += bed->s->arch_size / 8;
6828 free (cinfo.bitmask);
6829 free (cinfo.hashcodes);
6833 s = bfd_get_linker_section (dynobj, ".dynstr");
6834 BFD_ASSERT (s != NULL);
6836 elf_finalize_dynstr (output_bfd, info);
6838 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6840 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6841 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6848 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6851 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6854 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6855 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6858 /* Finish SHF_MERGE section merging. */
6861 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6866 if (!is_elf_hash_table (info->hash))
6869 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6870 if ((ibfd->flags & DYNAMIC) == 0
6871 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6872 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6873 == get_elf_backend_data (obfd)->s->elfclass))
6874 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6875 if ((sec->flags & SEC_MERGE) != 0
6876 && !bfd_is_abs_section (sec->output_section))
6878 struct bfd_elf_section_data *secdata;
6880 secdata = elf_section_data (sec);
6881 if (! _bfd_add_merge_section (obfd,
6882 &elf_hash_table (info)->merge_info,
6883 sec, &secdata->sec_info))
6885 else if (secdata->sec_info)
6886 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6889 if (elf_hash_table (info)->merge_info != NULL)
6890 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6891 merge_sections_remove_hook);
6895 /* Create an entry in an ELF linker hash table. */
6897 struct bfd_hash_entry *
6898 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6899 struct bfd_hash_table *table,
6902 /* Allocate the structure if it has not already been allocated by a
6906 entry = (struct bfd_hash_entry *)
6907 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6912 /* Call the allocation method of the superclass. */
6913 entry = _bfd_link_hash_newfunc (entry, table, string);
6916 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6917 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6919 /* Set local fields. */
6922 ret->got = htab->init_got_refcount;
6923 ret->plt = htab->init_plt_refcount;
6924 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6925 - offsetof (struct elf_link_hash_entry, size)));
6926 /* Assume that we have been called by a non-ELF symbol reader.
6927 This flag is then reset by the code which reads an ELF input
6928 file. This ensures that a symbol created by a non-ELF symbol
6929 reader will have the flag set correctly. */
6936 /* Copy data from an indirect symbol to its direct symbol, hiding the
6937 old indirect symbol. Also used for copying flags to a weakdef. */
6940 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6941 struct elf_link_hash_entry *dir,
6942 struct elf_link_hash_entry *ind)
6944 struct elf_link_hash_table *htab;
6946 /* Copy down any references that we may have already seen to the
6947 symbol which just became indirect if DIR isn't a hidden versioned
6950 if (dir->versioned != versioned_hidden)
6952 dir->ref_dynamic |= ind->ref_dynamic;
6953 dir->ref_regular |= ind->ref_regular;
6954 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6955 dir->non_got_ref |= ind->non_got_ref;
6956 dir->needs_plt |= ind->needs_plt;
6957 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6960 if (ind->root.type != bfd_link_hash_indirect)
6963 /* Copy over the global and procedure linkage table refcount entries.
6964 These may have been already set up by a check_relocs routine. */
6965 htab = elf_hash_table (info);
6966 if (ind->got.refcount > htab->init_got_refcount.refcount)
6968 if (dir->got.refcount < 0)
6969 dir->got.refcount = 0;
6970 dir->got.refcount += ind->got.refcount;
6971 ind->got.refcount = htab->init_got_refcount.refcount;
6974 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6976 if (dir->plt.refcount < 0)
6977 dir->plt.refcount = 0;
6978 dir->plt.refcount += ind->plt.refcount;
6979 ind->plt.refcount = htab->init_plt_refcount.refcount;
6982 if (ind->dynindx != -1)
6984 if (dir->dynindx != -1)
6985 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6986 dir->dynindx = ind->dynindx;
6987 dir->dynstr_index = ind->dynstr_index;
6989 ind->dynstr_index = 0;
6994 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6995 struct elf_link_hash_entry *h,
6996 bfd_boolean force_local)
6998 /* STT_GNU_IFUNC symbol must go through PLT. */
6999 if (h->type != STT_GNU_IFUNC)
7001 h->plt = elf_hash_table (info)->init_plt_offset;
7006 h->forced_local = 1;
7007 if (h->dynindx != -1)
7010 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7016 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7020 _bfd_elf_link_hash_table_init
7021 (struct elf_link_hash_table *table,
7023 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7024 struct bfd_hash_table *,
7026 unsigned int entsize,
7027 enum elf_target_id target_id)
7030 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7032 table->init_got_refcount.refcount = can_refcount - 1;
7033 table->init_plt_refcount.refcount = can_refcount - 1;
7034 table->init_got_offset.offset = -(bfd_vma) 1;
7035 table->init_plt_offset.offset = -(bfd_vma) 1;
7036 /* The first dynamic symbol is a dummy. */
7037 table->dynsymcount = 1;
7039 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7041 table->root.type = bfd_link_elf_hash_table;
7042 table->hash_table_id = target_id;
7047 /* Create an ELF linker hash table. */
7049 struct bfd_link_hash_table *
7050 _bfd_elf_link_hash_table_create (bfd *abfd)
7052 struct elf_link_hash_table *ret;
7053 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7055 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7059 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7060 sizeof (struct elf_link_hash_entry),
7066 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7071 /* Destroy an ELF linker hash table. */
7074 _bfd_elf_link_hash_table_free (bfd *obfd)
7076 struct elf_link_hash_table *htab;
7078 htab = (struct elf_link_hash_table *) obfd->link.hash;
7079 if (htab->dynstr != NULL)
7080 _bfd_elf_strtab_free (htab->dynstr);
7081 _bfd_merge_sections_free (htab->merge_info);
7082 _bfd_generic_link_hash_table_free (obfd);
7085 /* This is a hook for the ELF emulation code in the generic linker to
7086 tell the backend linker what file name to use for the DT_NEEDED
7087 entry for a dynamic object. */
7090 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7092 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7093 && bfd_get_format (abfd) == bfd_object)
7094 elf_dt_name (abfd) = name;
7098 bfd_elf_get_dyn_lib_class (bfd *abfd)
7101 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7102 && bfd_get_format (abfd) == bfd_object)
7103 lib_class = elf_dyn_lib_class (abfd);
7110 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7112 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7113 && bfd_get_format (abfd) == bfd_object)
7114 elf_dyn_lib_class (abfd) = lib_class;
7117 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7118 the linker ELF emulation code. */
7120 struct bfd_link_needed_list *
7121 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7122 struct bfd_link_info *info)
7124 if (! is_elf_hash_table (info->hash))
7126 return elf_hash_table (info)->needed;
7129 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7130 hook for the linker ELF emulation code. */
7132 struct bfd_link_needed_list *
7133 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7134 struct bfd_link_info *info)
7136 if (! is_elf_hash_table (info->hash))
7138 return elf_hash_table (info)->runpath;
7141 /* Get the name actually used for a dynamic object for a link. This
7142 is the SONAME entry if there is one. Otherwise, it is the string
7143 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7146 bfd_elf_get_dt_soname (bfd *abfd)
7148 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7149 && bfd_get_format (abfd) == bfd_object)
7150 return elf_dt_name (abfd);
7154 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7155 the ELF linker emulation code. */
7158 bfd_elf_get_bfd_needed_list (bfd *abfd,
7159 struct bfd_link_needed_list **pneeded)
7162 bfd_byte *dynbuf = NULL;
7163 unsigned int elfsec;
7164 unsigned long shlink;
7165 bfd_byte *extdyn, *extdynend;
7167 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7171 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7172 || bfd_get_format (abfd) != bfd_object)
7175 s = bfd_get_section_by_name (abfd, ".dynamic");
7176 if (s == NULL || s->size == 0)
7179 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7182 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7183 if (elfsec == SHN_BAD)
7186 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7188 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7189 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7192 extdynend = extdyn + s->size;
7193 for (; extdyn < extdynend; extdyn += extdynsize)
7195 Elf_Internal_Dyn dyn;
7197 (*swap_dyn_in) (abfd, extdyn, &dyn);
7199 if (dyn.d_tag == DT_NULL)
7202 if (dyn.d_tag == DT_NEEDED)
7205 struct bfd_link_needed_list *l;
7206 unsigned int tagv = dyn.d_un.d_val;
7209 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7214 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7235 struct elf_symbuf_symbol
7237 unsigned long st_name; /* Symbol name, index in string tbl */
7238 unsigned char st_info; /* Type and binding attributes */
7239 unsigned char st_other; /* Visibilty, and target specific */
7242 struct elf_symbuf_head
7244 struct elf_symbuf_symbol *ssym;
7245 bfd_size_type count;
7246 unsigned int st_shndx;
7253 Elf_Internal_Sym *isym;
7254 struct elf_symbuf_symbol *ssym;
7259 /* Sort references to symbols by ascending section number. */
7262 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7264 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7265 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7267 return s1->st_shndx - s2->st_shndx;
7271 elf_sym_name_compare (const void *arg1, const void *arg2)
7273 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7274 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7275 return strcmp (s1->name, s2->name);
7278 static struct elf_symbuf_head *
7279 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7281 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7282 struct elf_symbuf_symbol *ssym;
7283 struct elf_symbuf_head *ssymbuf, *ssymhead;
7284 bfd_size_type i, shndx_count, total_size;
7286 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7290 for (ind = indbuf, i = 0; i < symcount; i++)
7291 if (isymbuf[i].st_shndx != SHN_UNDEF)
7292 *ind++ = &isymbuf[i];
7295 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7296 elf_sort_elf_symbol);
7299 if (indbufend > indbuf)
7300 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7301 if (ind[0]->st_shndx != ind[1]->st_shndx)
7304 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7305 + (indbufend - indbuf) * sizeof (*ssym));
7306 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7307 if (ssymbuf == NULL)
7313 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7314 ssymbuf->ssym = NULL;
7315 ssymbuf->count = shndx_count;
7316 ssymbuf->st_shndx = 0;
7317 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7319 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7322 ssymhead->ssym = ssym;
7323 ssymhead->count = 0;
7324 ssymhead->st_shndx = (*ind)->st_shndx;
7326 ssym->st_name = (*ind)->st_name;
7327 ssym->st_info = (*ind)->st_info;
7328 ssym->st_other = (*ind)->st_other;
7331 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7332 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7339 /* Check if 2 sections define the same set of local and global
7343 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7344 struct bfd_link_info *info)
7347 const struct elf_backend_data *bed1, *bed2;
7348 Elf_Internal_Shdr *hdr1, *hdr2;
7349 bfd_size_type symcount1, symcount2;
7350 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7351 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7352 Elf_Internal_Sym *isym, *isymend;
7353 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7354 bfd_size_type count1, count2, i;
7355 unsigned int shndx1, shndx2;
7361 /* Both sections have to be in ELF. */
7362 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7363 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7366 if (elf_section_type (sec1) != elf_section_type (sec2))
7369 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7370 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7371 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7374 bed1 = get_elf_backend_data (bfd1);
7375 bed2 = get_elf_backend_data (bfd2);
7376 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7377 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7378 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7379 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7381 if (symcount1 == 0 || symcount2 == 0)
7387 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7388 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7390 if (ssymbuf1 == NULL)
7392 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7394 if (isymbuf1 == NULL)
7397 if (!info->reduce_memory_overheads)
7398 elf_tdata (bfd1)->symbuf = ssymbuf1
7399 = elf_create_symbuf (symcount1, isymbuf1);
7402 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7404 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7406 if (isymbuf2 == NULL)
7409 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7410 elf_tdata (bfd2)->symbuf = ssymbuf2
7411 = elf_create_symbuf (symcount2, isymbuf2);
7414 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7416 /* Optimized faster version. */
7417 bfd_size_type lo, hi, mid;
7418 struct elf_symbol *symp;
7419 struct elf_symbuf_symbol *ssym, *ssymend;
7422 hi = ssymbuf1->count;
7427 mid = (lo + hi) / 2;
7428 if (shndx1 < ssymbuf1[mid].st_shndx)
7430 else if (shndx1 > ssymbuf1[mid].st_shndx)
7434 count1 = ssymbuf1[mid].count;
7441 hi = ssymbuf2->count;
7446 mid = (lo + hi) / 2;
7447 if (shndx2 < ssymbuf2[mid].st_shndx)
7449 else if (shndx2 > ssymbuf2[mid].st_shndx)
7453 count2 = ssymbuf2[mid].count;
7459 if (count1 == 0 || count2 == 0 || count1 != count2)
7463 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7465 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7466 if (symtable1 == NULL || symtable2 == NULL)
7470 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7471 ssym < ssymend; ssym++, symp++)
7473 symp->u.ssym = ssym;
7474 symp->name = bfd_elf_string_from_elf_section (bfd1,
7480 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7481 ssym < ssymend; ssym++, symp++)
7483 symp->u.ssym = ssym;
7484 symp->name = bfd_elf_string_from_elf_section (bfd2,
7489 /* Sort symbol by name. */
7490 qsort (symtable1, count1, sizeof (struct elf_symbol),
7491 elf_sym_name_compare);
7492 qsort (symtable2, count1, sizeof (struct elf_symbol),
7493 elf_sym_name_compare);
7495 for (i = 0; i < count1; i++)
7496 /* Two symbols must have the same binding, type and name. */
7497 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7498 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7499 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7506 symtable1 = (struct elf_symbol *)
7507 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7508 symtable2 = (struct elf_symbol *)
7509 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7510 if (symtable1 == NULL || symtable2 == NULL)
7513 /* Count definitions in the section. */
7515 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7516 if (isym->st_shndx == shndx1)
7517 symtable1[count1++].u.isym = isym;
7520 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7521 if (isym->st_shndx == shndx2)
7522 symtable2[count2++].u.isym = isym;
7524 if (count1 == 0 || count2 == 0 || count1 != count2)
7527 for (i = 0; i < count1; i++)
7529 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7530 symtable1[i].u.isym->st_name);
7532 for (i = 0; i < count2; i++)
7534 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7535 symtable2[i].u.isym->st_name);
7537 /* Sort symbol by name. */
7538 qsort (symtable1, count1, sizeof (struct elf_symbol),
7539 elf_sym_name_compare);
7540 qsort (symtable2, count1, sizeof (struct elf_symbol),
7541 elf_sym_name_compare);
7543 for (i = 0; i < count1; i++)
7544 /* Two symbols must have the same binding, type and name. */
7545 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7546 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7547 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7565 /* Return TRUE if 2 section types are compatible. */
7568 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7569 bfd *bbfd, const asection *bsec)
7573 || abfd->xvec->flavour != bfd_target_elf_flavour
7574 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7577 return elf_section_type (asec) == elf_section_type (bsec);
7580 /* Final phase of ELF linker. */
7582 /* A structure we use to avoid passing large numbers of arguments. */
7584 struct elf_final_link_info
7586 /* General link information. */
7587 struct bfd_link_info *info;
7590 /* Symbol string table. */
7591 struct elf_strtab_hash *symstrtab;
7592 /* .hash section. */
7594 /* symbol version section (.gnu.version). */
7595 asection *symver_sec;
7596 /* Buffer large enough to hold contents of any section. */
7598 /* Buffer large enough to hold external relocs of any section. */
7599 void *external_relocs;
7600 /* Buffer large enough to hold internal relocs of any section. */
7601 Elf_Internal_Rela *internal_relocs;
7602 /* Buffer large enough to hold external local symbols of any input
7604 bfd_byte *external_syms;
7605 /* And a buffer for symbol section indices. */
7606 Elf_External_Sym_Shndx *locsym_shndx;
7607 /* Buffer large enough to hold internal local symbols of any input
7609 Elf_Internal_Sym *internal_syms;
7610 /* Array large enough to hold a symbol index for each local symbol
7611 of any input BFD. */
7613 /* Array large enough to hold a section pointer for each local
7614 symbol of any input BFD. */
7615 asection **sections;
7616 /* Buffer for SHT_SYMTAB_SHNDX section. */
7617 Elf_External_Sym_Shndx *symshndxbuf;
7618 /* Number of STT_FILE syms seen. */
7619 size_t filesym_count;
7622 /* This struct is used to pass information to elf_link_output_extsym. */
7624 struct elf_outext_info
7627 bfd_boolean localsyms;
7628 bfd_boolean file_sym_done;
7629 struct elf_final_link_info *flinfo;
7633 /* Support for evaluating a complex relocation.
7635 Complex relocations are generalized, self-describing relocations. The
7636 implementation of them consists of two parts: complex symbols, and the
7637 relocations themselves.
7639 The relocations are use a reserved elf-wide relocation type code (R_RELC
7640 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7641 information (start bit, end bit, word width, etc) into the addend. This
7642 information is extracted from CGEN-generated operand tables within gas.
7644 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7645 internal) representing prefix-notation expressions, including but not
7646 limited to those sorts of expressions normally encoded as addends in the
7647 addend field. The symbol mangling format is:
7650 | <unary-operator> ':' <node>
7651 | <binary-operator> ':' <node> ':' <node>
7654 <literal> := 's' <digits=N> ':' <N character symbol name>
7655 | 'S' <digits=N> ':' <N character section name>
7659 <binary-operator> := as in C
7660 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7663 set_symbol_value (bfd *bfd_with_globals,
7664 Elf_Internal_Sym *isymbuf,
7669 struct elf_link_hash_entry **sym_hashes;
7670 struct elf_link_hash_entry *h;
7671 size_t extsymoff = locsymcount;
7673 if (symidx < locsymcount)
7675 Elf_Internal_Sym *sym;
7677 sym = isymbuf + symidx;
7678 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7680 /* It is a local symbol: move it to the
7681 "absolute" section and give it a value. */
7682 sym->st_shndx = SHN_ABS;
7683 sym->st_value = val;
7686 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7690 /* It is a global symbol: set its link type
7691 to "defined" and give it a value. */
7693 sym_hashes = elf_sym_hashes (bfd_with_globals);
7694 h = sym_hashes [symidx - extsymoff];
7695 while (h->root.type == bfd_link_hash_indirect
7696 || h->root.type == bfd_link_hash_warning)
7697 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7698 h->root.type = bfd_link_hash_defined;
7699 h->root.u.def.value = val;
7700 h->root.u.def.section = bfd_abs_section_ptr;
7704 resolve_symbol (const char *name,
7706 struct elf_final_link_info *flinfo,
7708 Elf_Internal_Sym *isymbuf,
7711 Elf_Internal_Sym *sym;
7712 struct bfd_link_hash_entry *global_entry;
7713 const char *candidate = NULL;
7714 Elf_Internal_Shdr *symtab_hdr;
7717 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7719 for (i = 0; i < locsymcount; ++ i)
7723 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7726 candidate = bfd_elf_string_from_elf_section (input_bfd,
7727 symtab_hdr->sh_link,
7730 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7731 name, candidate, (unsigned long) sym->st_value);
7733 if (candidate && strcmp (candidate, name) == 0)
7735 asection *sec = flinfo->sections [i];
7737 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7738 *result += sec->output_offset + sec->output_section->vma;
7740 printf ("Found symbol with value %8.8lx\n",
7741 (unsigned long) *result);
7747 /* Hmm, haven't found it yet. perhaps it is a global. */
7748 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7749 FALSE, FALSE, TRUE);
7753 if (global_entry->type == bfd_link_hash_defined
7754 || global_entry->type == bfd_link_hash_defweak)
7756 *result = (global_entry->u.def.value
7757 + global_entry->u.def.section->output_section->vma
7758 + global_entry->u.def.section->output_offset);
7760 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7761 global_entry->root.string, (unsigned long) *result);
7769 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7770 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7771 names like "foo.end" which is the end address of section "foo". */
7774 resolve_section (const char *name,
7782 for (curr = sections; curr; curr = curr->next)
7783 if (strcmp (curr->name, name) == 0)
7785 *result = curr->vma;
7789 /* Hmm. still haven't found it. try pseudo-section names. */
7790 /* FIXME: This could be coded more efficiently... */
7791 for (curr = sections; curr; curr = curr->next)
7793 len = strlen (curr->name);
7794 if (len > strlen (name))
7797 if (strncmp (curr->name, name, len) == 0)
7799 if (strncmp (".end", name + len, 4) == 0)
7801 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7805 /* Insert more pseudo-section names here, if you like. */
7813 undefined_reference (const char *reftype, const char *name)
7815 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7820 eval_symbol (bfd_vma *result,
7823 struct elf_final_link_info *flinfo,
7825 Elf_Internal_Sym *isymbuf,
7834 const char *sym = *symp;
7836 bfd_boolean symbol_is_section = FALSE;
7841 if (len < 1 || len > sizeof (symbuf))
7843 bfd_set_error (bfd_error_invalid_operation);
7856 *result = strtoul (sym, (char **) symp, 16);
7860 symbol_is_section = TRUE;
7863 symlen = strtol (sym, (char **) symp, 10);
7864 sym = *symp + 1; /* Skip the trailing ':'. */
7866 if (symend < sym || symlen + 1 > sizeof (symbuf))
7868 bfd_set_error (bfd_error_invalid_operation);
7872 memcpy (symbuf, sym, symlen);
7873 symbuf[symlen] = '\0';
7874 *symp = sym + symlen;
7876 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7877 the symbol as a section, or vice-versa. so we're pretty liberal in our
7878 interpretation here; section means "try section first", not "must be a
7879 section", and likewise with symbol. */
7881 if (symbol_is_section)
7883 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
7884 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7885 isymbuf, locsymcount))
7887 undefined_reference ("section", symbuf);
7893 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7894 isymbuf, locsymcount)
7895 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7898 undefined_reference ("symbol", symbuf);
7905 /* All that remains are operators. */
7907 #define UNARY_OP(op) \
7908 if (strncmp (sym, #op, strlen (#op)) == 0) \
7910 sym += strlen (#op); \
7914 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7915 isymbuf, locsymcount, signed_p)) \
7918 *result = op ((bfd_signed_vma) a); \
7924 #define BINARY_OP(op) \
7925 if (strncmp (sym, #op, strlen (#op)) == 0) \
7927 sym += strlen (#op); \
7931 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7932 isymbuf, locsymcount, signed_p)) \
7935 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7936 isymbuf, locsymcount, signed_p)) \
7939 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7969 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7970 bfd_set_error (bfd_error_invalid_operation);
7976 put_value (bfd_vma size,
7977 unsigned long chunksz,
7982 location += (size - chunksz);
7984 for (; size; size -= chunksz, location -= chunksz)
7989 bfd_put_8 (input_bfd, x, location);
7993 bfd_put_16 (input_bfd, x, location);
7997 bfd_put_32 (input_bfd, x, location);
7998 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8004 bfd_put_64 (input_bfd, x, location);
8005 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8018 get_value (bfd_vma size,
8019 unsigned long chunksz,
8026 /* Sanity checks. */
8027 BFD_ASSERT (chunksz <= sizeof (x)
8030 && (size % chunksz) == 0
8031 && input_bfd != NULL
8032 && location != NULL);
8034 if (chunksz == sizeof (x))
8036 BFD_ASSERT (size == chunksz);
8038 /* Make sure that we do not perform an undefined shift operation.
8039 We know that size == chunksz so there will only be one iteration
8040 of the loop below. */
8044 shift = 8 * chunksz;
8046 for (; size; size -= chunksz, location += chunksz)
8051 x = (x << shift) | bfd_get_8 (input_bfd, location);
8054 x = (x << shift) | bfd_get_16 (input_bfd, location);
8057 x = (x << shift) | bfd_get_32 (input_bfd, location);
8061 x = (x << shift) | bfd_get_64 (input_bfd, location);
8072 decode_complex_addend (unsigned long *start, /* in bits */
8073 unsigned long *oplen, /* in bits */
8074 unsigned long *len, /* in bits */
8075 unsigned long *wordsz, /* in bytes */
8076 unsigned long *chunksz, /* in bytes */
8077 unsigned long *lsb0_p,
8078 unsigned long *signed_p,
8079 unsigned long *trunc_p,
8080 unsigned long encoded)
8082 * start = encoded & 0x3F;
8083 * len = (encoded >> 6) & 0x3F;
8084 * oplen = (encoded >> 12) & 0x3F;
8085 * wordsz = (encoded >> 18) & 0xF;
8086 * chunksz = (encoded >> 22) & 0xF;
8087 * lsb0_p = (encoded >> 27) & 1;
8088 * signed_p = (encoded >> 28) & 1;
8089 * trunc_p = (encoded >> 29) & 1;
8092 bfd_reloc_status_type
8093 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8094 asection *input_section ATTRIBUTE_UNUSED,
8096 Elf_Internal_Rela *rel,
8099 bfd_vma shift, x, mask;
8100 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8101 bfd_reloc_status_type r;
8103 /* Perform this reloc, since it is complex.
8104 (this is not to say that it necessarily refers to a complex
8105 symbol; merely that it is a self-describing CGEN based reloc.
8106 i.e. the addend has the complete reloc information (bit start, end,
8107 word size, etc) encoded within it.). */
8109 decode_complex_addend (&start, &oplen, &len, &wordsz,
8110 &chunksz, &lsb0_p, &signed_p,
8111 &trunc_p, rel->r_addend);
8113 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8116 shift = (start + 1) - len;
8118 shift = (8 * wordsz) - (start + len);
8120 x = get_value (wordsz, chunksz, input_bfd,
8121 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8124 printf ("Doing complex reloc: "
8125 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8126 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8127 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8128 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8129 oplen, (unsigned long) x, (unsigned long) mask,
8130 (unsigned long) relocation);
8135 /* Now do an overflow check. */
8136 r = bfd_check_overflow ((signed_p
8137 ? complain_overflow_signed
8138 : complain_overflow_unsigned),
8139 len, 0, (8 * wordsz),
8143 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8146 printf (" relocation: %8.8lx\n"
8147 " shifted mask: %8.8lx\n"
8148 " shifted/masked reloc: %8.8lx\n"
8149 " result: %8.8lx\n",
8150 (unsigned long) relocation, (unsigned long) (mask << shift),
8151 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8153 put_value (wordsz, chunksz, input_bfd, x,
8154 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8158 /* Functions to read r_offset from external (target order) reloc
8159 entry. Faster than bfd_getl32 et al, because we let the compiler
8160 know the value is aligned. */
8163 ext32l_r_offset (const void *p)
8170 const union aligned32 *a
8171 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8173 uint32_t aval = ( (uint32_t) a->c[0]
8174 | (uint32_t) a->c[1] << 8
8175 | (uint32_t) a->c[2] << 16
8176 | (uint32_t) a->c[3] << 24);
8181 ext32b_r_offset (const void *p)
8188 const union aligned32 *a
8189 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8191 uint32_t aval = ( (uint32_t) a->c[0] << 24
8192 | (uint32_t) a->c[1] << 16
8193 | (uint32_t) a->c[2] << 8
8194 | (uint32_t) a->c[3]);
8198 #ifdef BFD_HOST_64_BIT
8200 ext64l_r_offset (const void *p)
8207 const union aligned64 *a
8208 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8210 uint64_t aval = ( (uint64_t) a->c[0]
8211 | (uint64_t) a->c[1] << 8
8212 | (uint64_t) a->c[2] << 16
8213 | (uint64_t) a->c[3] << 24
8214 | (uint64_t) a->c[4] << 32
8215 | (uint64_t) a->c[5] << 40
8216 | (uint64_t) a->c[6] << 48
8217 | (uint64_t) a->c[7] << 56);
8222 ext64b_r_offset (const void *p)
8229 const union aligned64 *a
8230 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8232 uint64_t aval = ( (uint64_t) a->c[0] << 56
8233 | (uint64_t) a->c[1] << 48
8234 | (uint64_t) a->c[2] << 40
8235 | (uint64_t) a->c[3] << 32
8236 | (uint64_t) a->c[4] << 24
8237 | (uint64_t) a->c[5] << 16
8238 | (uint64_t) a->c[6] << 8
8239 | (uint64_t) a->c[7]);
8244 /* When performing a relocatable link, the input relocations are
8245 preserved. But, if they reference global symbols, the indices
8246 referenced must be updated. Update all the relocations found in
8250 elf_link_adjust_relocs (bfd *abfd,
8251 struct bfd_elf_section_reloc_data *reldata,
8255 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8257 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8258 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8259 bfd_vma r_type_mask;
8261 unsigned int count = reldata->count;
8262 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8264 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8266 swap_in = bed->s->swap_reloc_in;
8267 swap_out = bed->s->swap_reloc_out;
8269 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8271 swap_in = bed->s->swap_reloca_in;
8272 swap_out = bed->s->swap_reloca_out;
8277 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8280 if (bed->s->arch_size == 32)
8287 r_type_mask = 0xffffffff;
8291 erela = reldata->hdr->contents;
8292 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8294 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8297 if (*rel_hash == NULL)
8300 BFD_ASSERT ((*rel_hash)->indx >= 0);
8302 (*swap_in) (abfd, erela, irela);
8303 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8304 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8305 | (irela[j].r_info & r_type_mask));
8306 (*swap_out) (abfd, irela, erela);
8309 if (sort && count != 0)
8311 bfd_vma (*ext_r_off) (const void *);
8314 bfd_byte *base, *end, *p, *loc;
8315 bfd_byte *buf = NULL;
8317 if (bed->s->arch_size == 32)
8319 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8320 ext_r_off = ext32l_r_offset;
8321 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8322 ext_r_off = ext32b_r_offset;
8328 #ifdef BFD_HOST_64_BIT
8329 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8330 ext_r_off = ext64l_r_offset;
8331 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8332 ext_r_off = ext64b_r_offset;
8338 /* Must use a stable sort here. A modified insertion sort,
8339 since the relocs are mostly sorted already. */
8340 elt_size = reldata->hdr->sh_entsize;
8341 base = reldata->hdr->contents;
8342 end = base + count * elt_size;
8343 if (elt_size > sizeof (Elf64_External_Rela))
8346 /* Ensure the first element is lowest. This acts as a sentinel,
8347 speeding the main loop below. */
8348 r_off = (*ext_r_off) (base);
8349 for (p = loc = base; (p += elt_size) < end; )
8351 bfd_vma r_off2 = (*ext_r_off) (p);
8360 /* Don't just swap *base and *loc as that changes the order
8361 of the original base[0] and base[1] if they happen to
8362 have the same r_offset. */
8363 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8364 memcpy (onebuf, loc, elt_size);
8365 memmove (base + elt_size, base, loc - base);
8366 memcpy (base, onebuf, elt_size);
8369 for (p = base + elt_size; (p += elt_size) < end; )
8371 /* base to p is sorted, *p is next to insert. */
8372 r_off = (*ext_r_off) (p);
8373 /* Search the sorted region for location to insert. */
8375 while (r_off < (*ext_r_off) (loc))
8380 /* Chances are there is a run of relocs to insert here,
8381 from one of more input files. Files are not always
8382 linked in order due to the way elf_link_input_bfd is
8383 called. See pr17666. */
8384 size_t sortlen = p - loc;
8385 bfd_vma r_off2 = (*ext_r_off) (loc);
8386 size_t runlen = elt_size;
8387 size_t buf_size = 96 * 1024;
8388 while (p + runlen < end
8389 && (sortlen <= buf_size
8390 || runlen + elt_size <= buf_size)
8391 && r_off2 > (*ext_r_off) (p + runlen))
8395 buf = bfd_malloc (buf_size);
8399 if (runlen < sortlen)
8401 memcpy (buf, p, runlen);
8402 memmove (loc + runlen, loc, sortlen);
8403 memcpy (loc, buf, runlen);
8407 memcpy (buf, loc, sortlen);
8408 memmove (loc, p, runlen);
8409 memcpy (loc + runlen, buf, sortlen);
8411 p += runlen - elt_size;
8414 /* Hashes are no longer valid. */
8415 free (reldata->hashes);
8416 reldata->hashes = NULL;
8422 struct elf_link_sort_rela
8428 enum elf_reloc_type_class type;
8429 /* We use this as an array of size int_rels_per_ext_rel. */
8430 Elf_Internal_Rela rela[1];
8434 elf_link_sort_cmp1 (const void *A, const void *B)
8436 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8437 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8438 int relativea, relativeb;
8440 relativea = a->type == reloc_class_relative;
8441 relativeb = b->type == reloc_class_relative;
8443 if (relativea < relativeb)
8445 if (relativea > relativeb)
8447 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8449 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8451 if (a->rela->r_offset < b->rela->r_offset)
8453 if (a->rela->r_offset > b->rela->r_offset)
8459 elf_link_sort_cmp2 (const void *A, const void *B)
8461 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8462 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8464 if (a->type < b->type)
8466 if (a->type > b->type)
8468 if (a->u.offset < b->u.offset)
8470 if (a->u.offset > b->u.offset)
8472 if (a->rela->r_offset < b->rela->r_offset)
8474 if (a->rela->r_offset > b->rela->r_offset)
8480 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8482 asection *dynamic_relocs;
8485 bfd_size_type count, size;
8486 size_t i, ret, sort_elt, ext_size;
8487 bfd_byte *sort, *s_non_relative, *p;
8488 struct elf_link_sort_rela *sq;
8489 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8490 int i2e = bed->s->int_rels_per_ext_rel;
8491 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8492 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8493 struct bfd_link_order *lo;
8495 bfd_boolean use_rela;
8497 /* Find a dynamic reloc section. */
8498 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8499 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8500 if (rela_dyn != NULL && rela_dyn->size > 0
8501 && rel_dyn != NULL && rel_dyn->size > 0)
8503 bfd_boolean use_rela_initialised = FALSE;
8505 /* This is just here to stop gcc from complaining.
8506 It's initialization checking code is not perfect. */
8509 /* Both sections are present. Examine the sizes
8510 of the indirect sections to help us choose. */
8511 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8512 if (lo->type == bfd_indirect_link_order)
8514 asection *o = lo->u.indirect.section;
8516 if ((o->size % bed->s->sizeof_rela) == 0)
8518 if ((o->size % bed->s->sizeof_rel) == 0)
8519 /* Section size is divisible by both rel and rela sizes.
8520 It is of no help to us. */
8524 /* Section size is only divisible by rela. */
8525 if (use_rela_initialised && (use_rela == FALSE))
8528 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8529 bfd_set_error (bfd_error_invalid_operation);
8535 use_rela_initialised = TRUE;
8539 else if ((o->size % bed->s->sizeof_rel) == 0)
8541 /* Section size is only divisible by rel. */
8542 if (use_rela_initialised && (use_rela == TRUE))
8545 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8546 bfd_set_error (bfd_error_invalid_operation);
8552 use_rela_initialised = TRUE;
8557 /* The section size is not divisible by either - something is wrong. */
8559 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8560 bfd_set_error (bfd_error_invalid_operation);
8565 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8566 if (lo->type == bfd_indirect_link_order)
8568 asection *o = lo->u.indirect.section;
8570 if ((o->size % bed->s->sizeof_rela) == 0)
8572 if ((o->size % bed->s->sizeof_rel) == 0)
8573 /* Section size is divisible by both rel and rela sizes.
8574 It is of no help to us. */
8578 /* Section size is only divisible by rela. */
8579 if (use_rela_initialised && (use_rela == FALSE))
8582 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8583 bfd_set_error (bfd_error_invalid_operation);
8589 use_rela_initialised = TRUE;
8593 else if ((o->size % bed->s->sizeof_rel) == 0)
8595 /* Section size is only divisible by rel. */
8596 if (use_rela_initialised && (use_rela == TRUE))
8599 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8600 bfd_set_error (bfd_error_invalid_operation);
8606 use_rela_initialised = TRUE;
8611 /* The section size is not divisible by either - something is wrong. */
8613 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8614 bfd_set_error (bfd_error_invalid_operation);
8619 if (! use_rela_initialised)
8623 else if (rela_dyn != NULL && rela_dyn->size > 0)
8625 else if (rel_dyn != NULL && rel_dyn->size > 0)
8632 dynamic_relocs = rela_dyn;
8633 ext_size = bed->s->sizeof_rela;
8634 swap_in = bed->s->swap_reloca_in;
8635 swap_out = bed->s->swap_reloca_out;
8639 dynamic_relocs = rel_dyn;
8640 ext_size = bed->s->sizeof_rel;
8641 swap_in = bed->s->swap_reloc_in;
8642 swap_out = bed->s->swap_reloc_out;
8646 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8647 if (lo->type == bfd_indirect_link_order)
8648 size += lo->u.indirect.section->size;
8650 if (size != dynamic_relocs->size)
8653 sort_elt = (sizeof (struct elf_link_sort_rela)
8654 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8656 count = dynamic_relocs->size / ext_size;
8659 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8663 (*info->callbacks->warning)
8664 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8668 if (bed->s->arch_size == 32)
8669 r_sym_mask = ~(bfd_vma) 0xff;
8671 r_sym_mask = ~(bfd_vma) 0xffffffff;
8673 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8674 if (lo->type == bfd_indirect_link_order)
8676 bfd_byte *erel, *erelend;
8677 asection *o = lo->u.indirect.section;
8679 if (o->contents == NULL && o->size != 0)
8681 /* This is a reloc section that is being handled as a normal
8682 section. See bfd_section_from_shdr. We can't combine
8683 relocs in this case. */
8688 erelend = o->contents + o->size;
8689 /* FIXME: octets_per_byte. */
8690 p = sort + o->output_offset / ext_size * sort_elt;
8692 while (erel < erelend)
8694 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8696 (*swap_in) (abfd, erel, s->rela);
8697 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8698 s->u.sym_mask = r_sym_mask;
8704 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8706 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8708 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8709 if (s->type != reloc_class_relative)
8715 sq = (struct elf_link_sort_rela *) s_non_relative;
8716 for (; i < count; i++, p += sort_elt)
8718 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8719 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8721 sp->u.offset = sq->rela->r_offset;
8724 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8726 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8727 if (lo->type == bfd_indirect_link_order)
8729 bfd_byte *erel, *erelend;
8730 asection *o = lo->u.indirect.section;
8733 erelend = o->contents + o->size;
8734 /* FIXME: octets_per_byte. */
8735 p = sort + o->output_offset / ext_size * sort_elt;
8736 while (erel < erelend)
8738 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8739 (*swap_out) (abfd, s->rela, erel);
8746 *psec = dynamic_relocs;
8750 /* Add a symbol to the output symbol string table. */
8753 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8755 Elf_Internal_Sym *elfsym,
8756 asection *input_sec,
8757 struct elf_link_hash_entry *h)
8759 int (*output_symbol_hook)
8760 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8761 struct elf_link_hash_entry *);
8762 struct elf_link_hash_table *hash_table;
8763 const struct elf_backend_data *bed;
8764 bfd_size_type strtabsize;
8766 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8768 bed = get_elf_backend_data (flinfo->output_bfd);
8769 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8770 if (output_symbol_hook != NULL)
8772 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8779 || (input_sec->flags & SEC_EXCLUDE))
8780 elfsym->st_name = (unsigned long) -1;
8783 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8784 to get the final offset for st_name. */
8786 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8788 if (elfsym->st_name == (unsigned long) -1)
8792 hash_table = elf_hash_table (flinfo->info);
8793 strtabsize = hash_table->strtabsize;
8794 if (strtabsize <= hash_table->strtabcount)
8796 strtabsize += strtabsize;
8797 hash_table->strtabsize = strtabsize;
8798 strtabsize *= sizeof (*hash_table->strtab);
8800 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8802 if (hash_table->strtab == NULL)
8805 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8806 hash_table->strtab[hash_table->strtabcount].dest_index
8807 = hash_table->strtabcount;
8808 hash_table->strtab[hash_table->strtabcount].destshndx_index
8809 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8811 bfd_get_symcount (flinfo->output_bfd) += 1;
8812 hash_table->strtabcount += 1;
8817 /* Swap symbols out to the symbol table and flush the output symbols to
8821 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8823 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8824 bfd_size_type amt, i;
8825 const struct elf_backend_data *bed;
8827 Elf_Internal_Shdr *hdr;
8831 if (!hash_table->strtabcount)
8834 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8836 bed = get_elf_backend_data (flinfo->output_bfd);
8838 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8839 symbuf = (bfd_byte *) bfd_malloc (amt);
8843 if (flinfo->symshndxbuf)
8845 amt = (sizeof (Elf_External_Sym_Shndx)
8846 * (bfd_get_symcount (flinfo->output_bfd)));
8847 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8848 if (flinfo->symshndxbuf == NULL)
8855 for (i = 0; i < hash_table->strtabcount; i++)
8857 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8858 if (elfsym->sym.st_name == (unsigned long) -1)
8859 elfsym->sym.st_name = 0;
8862 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8863 elfsym->sym.st_name);
8864 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8865 ((bfd_byte *) symbuf
8866 + (elfsym->dest_index
8867 * bed->s->sizeof_sym)),
8868 (flinfo->symshndxbuf
8869 + elfsym->destshndx_index));
8872 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8873 pos = hdr->sh_offset + hdr->sh_size;
8874 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8875 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8876 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8878 hdr->sh_size += amt;
8886 free (hash_table->strtab);
8887 hash_table->strtab = NULL;
8892 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8895 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8897 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8898 && sym->st_shndx < SHN_LORESERVE)
8900 /* The gABI doesn't support dynamic symbols in output sections
8902 (*_bfd_error_handler)
8903 (_("%B: Too many sections: %d (>= %d)"),
8904 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8905 bfd_set_error (bfd_error_nonrepresentable_section);
8911 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8912 allowing an unsatisfied unversioned symbol in the DSO to match a
8913 versioned symbol that would normally require an explicit version.
8914 We also handle the case that a DSO references a hidden symbol
8915 which may be satisfied by a versioned symbol in another DSO. */
8918 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8919 const struct elf_backend_data *bed,
8920 struct elf_link_hash_entry *h)
8923 struct elf_link_loaded_list *loaded;
8925 if (!is_elf_hash_table (info->hash))
8928 /* Check indirect symbol. */
8929 while (h->root.type == bfd_link_hash_indirect)
8930 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8932 switch (h->root.type)
8938 case bfd_link_hash_undefined:
8939 case bfd_link_hash_undefweak:
8940 abfd = h->root.u.undef.abfd;
8941 if ((abfd->flags & DYNAMIC) == 0
8942 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8946 case bfd_link_hash_defined:
8947 case bfd_link_hash_defweak:
8948 abfd = h->root.u.def.section->owner;
8951 case bfd_link_hash_common:
8952 abfd = h->root.u.c.p->section->owner;
8955 BFD_ASSERT (abfd != NULL);
8957 for (loaded = elf_hash_table (info)->loaded;
8959 loaded = loaded->next)
8962 Elf_Internal_Shdr *hdr;
8963 bfd_size_type symcount;
8964 bfd_size_type extsymcount;
8965 bfd_size_type extsymoff;
8966 Elf_Internal_Shdr *versymhdr;
8967 Elf_Internal_Sym *isym;
8968 Elf_Internal_Sym *isymend;
8969 Elf_Internal_Sym *isymbuf;
8970 Elf_External_Versym *ever;
8971 Elf_External_Versym *extversym;
8973 input = loaded->abfd;
8975 /* We check each DSO for a possible hidden versioned definition. */
8977 || (input->flags & DYNAMIC) == 0
8978 || elf_dynversym (input) == 0)
8981 hdr = &elf_tdata (input)->dynsymtab_hdr;
8983 symcount = hdr->sh_size / bed->s->sizeof_sym;
8984 if (elf_bad_symtab (input))
8986 extsymcount = symcount;
8991 extsymcount = symcount - hdr->sh_info;
8992 extsymoff = hdr->sh_info;
8995 if (extsymcount == 0)
8998 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9000 if (isymbuf == NULL)
9003 /* Read in any version definitions. */
9004 versymhdr = &elf_tdata (input)->dynversym_hdr;
9005 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9006 if (extversym == NULL)
9009 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9010 || (bfd_bread (extversym, versymhdr->sh_size, input)
9011 != versymhdr->sh_size))
9019 ever = extversym + extsymoff;
9020 isymend = isymbuf + extsymcount;
9021 for (isym = isymbuf; isym < isymend; isym++, ever++)
9024 Elf_Internal_Versym iver;
9025 unsigned short version_index;
9027 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9028 || isym->st_shndx == SHN_UNDEF)
9031 name = bfd_elf_string_from_elf_section (input,
9034 if (strcmp (name, h->root.root.string) != 0)
9037 _bfd_elf_swap_versym_in (input, ever, &iver);
9039 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9041 && h->forced_local))
9043 /* If we have a non-hidden versioned sym, then it should
9044 have provided a definition for the undefined sym unless
9045 it is defined in a non-shared object and forced local.
9050 version_index = iver.vs_vers & VERSYM_VERSION;
9051 if (version_index == 1 || version_index == 2)
9053 /* This is the base or first version. We can use it. */
9067 /* Convert ELF common symbol TYPE. */
9070 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9072 /* Commom symbol can only appear in relocatable link. */
9073 if (!bfd_link_relocatable (info))
9075 switch (info->elf_stt_common)
9079 case elf_stt_common:
9082 case no_elf_stt_common:
9089 /* Add an external symbol to the symbol table. This is called from
9090 the hash table traversal routine. When generating a shared object,
9091 we go through the symbol table twice. The first time we output
9092 anything that might have been forced to local scope in a version
9093 script. The second time we output the symbols that are still
9097 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9099 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9100 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9101 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9103 Elf_Internal_Sym sym;
9104 asection *input_sec;
9105 const struct elf_backend_data *bed;
9109 /* A symbol is bound locally if it is forced local or it is locally
9110 defined, hidden versioned, not referenced by shared library and
9111 not exported when linking executable. */
9112 bfd_boolean local_bind = (h->forced_local
9113 || (bfd_link_executable (flinfo->info)
9114 && !flinfo->info->export_dynamic
9118 && h->versioned == versioned_hidden));
9120 if (h->root.type == bfd_link_hash_warning)
9122 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9123 if (h->root.type == bfd_link_hash_new)
9127 /* Decide whether to output this symbol in this pass. */
9128 if (eoinfo->localsyms)
9139 bed = get_elf_backend_data (flinfo->output_bfd);
9141 if (h->root.type == bfd_link_hash_undefined)
9143 /* If we have an undefined symbol reference here then it must have
9144 come from a shared library that is being linked in. (Undefined
9145 references in regular files have already been handled unless
9146 they are in unreferenced sections which are removed by garbage
9148 bfd_boolean ignore_undef = FALSE;
9150 /* Some symbols may be special in that the fact that they're
9151 undefined can be safely ignored - let backend determine that. */
9152 if (bed->elf_backend_ignore_undef_symbol)
9153 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9155 /* If we are reporting errors for this situation then do so now. */
9158 && (!h->ref_regular || flinfo->info->gc_sections)
9159 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9160 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9162 if (!(flinfo->info->callbacks->undefined_symbol
9163 (flinfo->info, h->root.root.string,
9164 h->ref_regular ? NULL : h->root.u.undef.abfd,
9166 (flinfo->info->unresolved_syms_in_shared_libs
9167 == RM_GENERATE_ERROR))))
9169 bfd_set_error (bfd_error_bad_value);
9170 eoinfo->failed = TRUE;
9176 /* We should also warn if a forced local symbol is referenced from
9177 shared libraries. */
9178 if (bfd_link_executable (flinfo->info)
9183 && h->ref_dynamic_nonweak
9184 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9188 struct elf_link_hash_entry *hi = h;
9190 /* Check indirect symbol. */
9191 while (hi->root.type == bfd_link_hash_indirect)
9192 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9194 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9195 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9196 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9197 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9199 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9200 def_bfd = flinfo->output_bfd;
9201 if (hi->root.u.def.section != bfd_abs_section_ptr)
9202 def_bfd = hi->root.u.def.section->owner;
9203 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9204 h->root.root.string);
9205 bfd_set_error (bfd_error_bad_value);
9206 eoinfo->failed = TRUE;
9210 /* We don't want to output symbols that have never been mentioned by
9211 a regular file, or that we have been told to strip. However, if
9212 h->indx is set to -2, the symbol is used by a reloc and we must
9217 else if ((h->def_dynamic
9219 || h->root.type == bfd_link_hash_new)
9223 else if (flinfo->info->strip == strip_all)
9225 else if (flinfo->info->strip == strip_some
9226 && bfd_hash_lookup (flinfo->info->keep_hash,
9227 h->root.root.string, FALSE, FALSE) == NULL)
9229 else if ((h->root.type == bfd_link_hash_defined
9230 || h->root.type == bfd_link_hash_defweak)
9231 && ((flinfo->info->strip_discarded
9232 && discarded_section (h->root.u.def.section))
9233 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9234 && h->root.u.def.section->owner != NULL
9235 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9237 else if ((h->root.type == bfd_link_hash_undefined
9238 || h->root.type == bfd_link_hash_undefweak)
9239 && h->root.u.undef.abfd != NULL
9240 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9245 /* If we're stripping it, and it's not a dynamic symbol, there's
9246 nothing else to do. However, if it is a forced local symbol or
9247 an ifunc symbol we need to give the backend finish_dynamic_symbol
9248 function a chance to make it dynamic. */
9251 && type != STT_GNU_IFUNC
9252 && !h->forced_local)
9256 sym.st_size = h->size;
9257 sym.st_other = h->other;
9258 switch (h->root.type)
9261 case bfd_link_hash_new:
9262 case bfd_link_hash_warning:
9266 case bfd_link_hash_undefined:
9267 case bfd_link_hash_undefweak:
9268 input_sec = bfd_und_section_ptr;
9269 sym.st_shndx = SHN_UNDEF;
9272 case bfd_link_hash_defined:
9273 case bfd_link_hash_defweak:
9275 input_sec = h->root.u.def.section;
9276 if (input_sec->output_section != NULL)
9279 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9280 input_sec->output_section);
9281 if (sym.st_shndx == SHN_BAD)
9283 (*_bfd_error_handler)
9284 (_("%B: could not find output section %A for input section %A"),
9285 flinfo->output_bfd, input_sec->output_section, input_sec);
9286 bfd_set_error (bfd_error_nonrepresentable_section);
9287 eoinfo->failed = TRUE;
9291 /* ELF symbols in relocatable files are section relative,
9292 but in nonrelocatable files they are virtual
9294 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9295 if (!bfd_link_relocatable (flinfo->info))
9297 sym.st_value += input_sec->output_section->vma;
9298 if (h->type == STT_TLS)
9300 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9301 if (tls_sec != NULL)
9302 sym.st_value -= tls_sec->vma;
9308 BFD_ASSERT (input_sec->owner == NULL
9309 || (input_sec->owner->flags & DYNAMIC) != 0);
9310 sym.st_shndx = SHN_UNDEF;
9311 input_sec = bfd_und_section_ptr;
9316 case bfd_link_hash_common:
9317 input_sec = h->root.u.c.p->section;
9318 sym.st_shndx = bed->common_section_index (input_sec);
9319 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9322 case bfd_link_hash_indirect:
9323 /* These symbols are created by symbol versioning. They point
9324 to the decorated version of the name. For example, if the
9325 symbol foo@@GNU_1.2 is the default, which should be used when
9326 foo is used with no version, then we add an indirect symbol
9327 foo which points to foo@@GNU_1.2. We ignore these symbols,
9328 since the indirected symbol is already in the hash table. */
9332 if (type == STT_COMMON || type == STT_OBJECT)
9333 switch (h->root.type)
9335 case bfd_link_hash_common:
9336 type = elf_link_convert_common_type (flinfo->info, type);
9338 case bfd_link_hash_defined:
9339 case bfd_link_hash_defweak:
9340 if (bed->common_definition (&sym))
9341 type = elf_link_convert_common_type (flinfo->info, type);
9345 case bfd_link_hash_undefined:
9346 case bfd_link_hash_undefweak:
9354 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9355 /* Turn off visibility on local symbol. */
9356 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9358 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9359 else if (h->unique_global && h->def_regular)
9360 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9361 else if (h->root.type == bfd_link_hash_undefweak
9362 || h->root.type == bfd_link_hash_defweak)
9363 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9365 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9366 sym.st_target_internal = h->target_internal;
9368 /* Give the processor backend a chance to tweak the symbol value,
9369 and also to finish up anything that needs to be done for this
9370 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9371 forced local syms when non-shared is due to a historical quirk.
9372 STT_GNU_IFUNC symbol must go through PLT. */
9373 if ((h->type == STT_GNU_IFUNC
9375 && !bfd_link_relocatable (flinfo->info))
9376 || ((h->dynindx != -1
9378 && ((bfd_link_pic (flinfo->info)
9379 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9380 || h->root.type != bfd_link_hash_undefweak))
9381 || !h->forced_local)
9382 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9384 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9385 (flinfo->output_bfd, flinfo->info, h, &sym)))
9387 eoinfo->failed = TRUE;
9392 /* If we are marking the symbol as undefined, and there are no
9393 non-weak references to this symbol from a regular object, then
9394 mark the symbol as weak undefined; if there are non-weak
9395 references, mark the symbol as strong. We can't do this earlier,
9396 because it might not be marked as undefined until the
9397 finish_dynamic_symbol routine gets through with it. */
9398 if (sym.st_shndx == SHN_UNDEF
9400 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9401 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9404 type = ELF_ST_TYPE (sym.st_info);
9406 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9407 if (type == STT_GNU_IFUNC)
9410 if (h->ref_regular_nonweak)
9411 bindtype = STB_GLOBAL;
9413 bindtype = STB_WEAK;
9414 sym.st_info = ELF_ST_INFO (bindtype, type);
9417 /* If this is a symbol defined in a dynamic library, don't use the
9418 symbol size from the dynamic library. Relinking an executable
9419 against a new library may introduce gratuitous changes in the
9420 executable's symbols if we keep the size. */
9421 if (sym.st_shndx == SHN_UNDEF
9426 /* If a non-weak symbol with non-default visibility is not defined
9427 locally, it is a fatal error. */
9428 if (!bfd_link_relocatable (flinfo->info)
9429 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9430 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9431 && h->root.type == bfd_link_hash_undefined
9436 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9437 msg = _("%B: protected symbol `%s' isn't defined");
9438 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9439 msg = _("%B: internal symbol `%s' isn't defined");
9441 msg = _("%B: hidden symbol `%s' isn't defined");
9442 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9443 bfd_set_error (bfd_error_bad_value);
9444 eoinfo->failed = TRUE;
9448 /* If this symbol should be put in the .dynsym section, then put it
9449 there now. We already know the symbol index. We also fill in
9450 the entry in the .hash section. */
9451 if (elf_hash_table (flinfo->info)->dynsym != NULL
9453 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9457 /* Since there is no version information in the dynamic string,
9458 if there is no version info in symbol version section, we will
9459 have a run-time problem if not linking executable, referenced
9460 by shared library, not locally defined, or not bound locally.
9462 if (h->verinfo.verdef == NULL
9464 && (!bfd_link_executable (flinfo->info)
9466 || !h->def_regular))
9468 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9470 if (p && p [1] != '\0')
9472 (*_bfd_error_handler)
9473 (_("%B: No symbol version section for versioned symbol `%s'"),
9474 flinfo->output_bfd, h->root.root.string);
9475 eoinfo->failed = TRUE;
9480 sym.st_name = h->dynstr_index;
9481 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9482 + h->dynindx * bed->s->sizeof_sym);
9483 if (!check_dynsym (flinfo->output_bfd, &sym))
9485 eoinfo->failed = TRUE;
9488 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9490 if (flinfo->hash_sec != NULL)
9492 size_t hash_entry_size;
9493 bfd_byte *bucketpos;
9498 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9499 bucket = h->u.elf_hash_value % bucketcount;
9502 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9503 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9504 + (bucket + 2) * hash_entry_size);
9505 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9506 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9508 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9509 ((bfd_byte *) flinfo->hash_sec->contents
9510 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9513 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9515 Elf_Internal_Versym iversym;
9516 Elf_External_Versym *eversym;
9518 if (!h->def_regular)
9520 if (h->verinfo.verdef == NULL
9521 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9522 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9523 iversym.vs_vers = 0;
9525 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9529 if (h->verinfo.vertree == NULL)
9530 iversym.vs_vers = 1;
9532 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9533 if (flinfo->info->create_default_symver)
9537 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9539 if (h->versioned == versioned_hidden && h->def_regular)
9540 iversym.vs_vers |= VERSYM_HIDDEN;
9542 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9543 eversym += h->dynindx;
9544 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9548 /* If the symbol is undefined, and we didn't output it to .dynsym,
9549 strip it from .symtab too. Obviously we can't do this for
9550 relocatable output or when needed for --emit-relocs. */
9551 else if (input_sec == bfd_und_section_ptr
9553 && !bfd_link_relocatable (flinfo->info))
9555 /* Also strip others that we couldn't earlier due to dynamic symbol
9559 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9562 /* Output a FILE symbol so that following locals are not associated
9563 with the wrong input file. We need one for forced local symbols
9564 if we've seen more than one FILE symbol or when we have exactly
9565 one FILE symbol but global symbols are present in a file other
9566 than the one with the FILE symbol. We also need one if linker
9567 defined symbols are present. In practice these conditions are
9568 always met, so just emit the FILE symbol unconditionally. */
9569 if (eoinfo->localsyms
9570 && !eoinfo->file_sym_done
9571 && eoinfo->flinfo->filesym_count != 0)
9573 Elf_Internal_Sym fsym;
9575 memset (&fsym, 0, sizeof (fsym));
9576 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9577 fsym.st_shndx = SHN_ABS;
9578 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9579 bfd_und_section_ptr, NULL))
9582 eoinfo->file_sym_done = TRUE;
9585 indx = bfd_get_symcount (flinfo->output_bfd);
9586 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9590 eoinfo->failed = TRUE;
9595 else if (h->indx == -2)
9601 /* Return TRUE if special handling is done for relocs in SEC against
9602 symbols defined in discarded sections. */
9605 elf_section_ignore_discarded_relocs (asection *sec)
9607 const struct elf_backend_data *bed;
9609 switch (sec->sec_info_type)
9611 case SEC_INFO_TYPE_STABS:
9612 case SEC_INFO_TYPE_EH_FRAME:
9613 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9619 bed = get_elf_backend_data (sec->owner);
9620 if (bed->elf_backend_ignore_discarded_relocs != NULL
9621 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9627 /* Return a mask saying how ld should treat relocations in SEC against
9628 symbols defined in discarded sections. If this function returns
9629 COMPLAIN set, ld will issue a warning message. If this function
9630 returns PRETEND set, and the discarded section was link-once and the
9631 same size as the kept link-once section, ld will pretend that the
9632 symbol was actually defined in the kept section. Otherwise ld will
9633 zero the reloc (at least that is the intent, but some cooperation by
9634 the target dependent code is needed, particularly for REL targets). */
9637 _bfd_elf_default_action_discarded (asection *sec)
9639 if (sec->flags & SEC_DEBUGGING)
9642 if (strcmp (".eh_frame", sec->name) == 0)
9645 if (strcmp (".gcc_except_table", sec->name) == 0)
9648 return COMPLAIN | PRETEND;
9651 /* Find a match between a section and a member of a section group. */
9654 match_group_member (asection *sec, asection *group,
9655 struct bfd_link_info *info)
9657 asection *first = elf_next_in_group (group);
9658 asection *s = first;
9662 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9665 s = elf_next_in_group (s);
9673 /* Check if the kept section of a discarded section SEC can be used
9674 to replace it. Return the replacement if it is OK. Otherwise return
9678 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9682 kept = sec->kept_section;
9685 if ((kept->flags & SEC_GROUP) != 0)
9686 kept = match_group_member (sec, kept, info);
9688 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9689 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9691 sec->kept_section = kept;
9696 /* Link an input file into the linker output file. This function
9697 handles all the sections and relocations of the input file at once.
9698 This is so that we only have to read the local symbols once, and
9699 don't have to keep them in memory. */
9702 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9704 int (*relocate_section)
9705 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9706 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9708 Elf_Internal_Shdr *symtab_hdr;
9711 Elf_Internal_Sym *isymbuf;
9712 Elf_Internal_Sym *isym;
9713 Elf_Internal_Sym *isymend;
9715 asection **ppsection;
9717 const struct elf_backend_data *bed;
9718 struct elf_link_hash_entry **sym_hashes;
9719 bfd_size_type address_size;
9720 bfd_vma r_type_mask;
9722 bfd_boolean have_file_sym = FALSE;
9724 output_bfd = flinfo->output_bfd;
9725 bed = get_elf_backend_data (output_bfd);
9726 relocate_section = bed->elf_backend_relocate_section;
9728 /* If this is a dynamic object, we don't want to do anything here:
9729 we don't want the local symbols, and we don't want the section
9731 if ((input_bfd->flags & DYNAMIC) != 0)
9734 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9735 if (elf_bad_symtab (input_bfd))
9737 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9742 locsymcount = symtab_hdr->sh_info;
9743 extsymoff = symtab_hdr->sh_info;
9746 /* Read the local symbols. */
9747 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9748 if (isymbuf == NULL && locsymcount != 0)
9750 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9751 flinfo->internal_syms,
9752 flinfo->external_syms,
9753 flinfo->locsym_shndx);
9754 if (isymbuf == NULL)
9758 /* Find local symbol sections and adjust values of symbols in
9759 SEC_MERGE sections. Write out those local symbols we know are
9760 going into the output file. */
9761 isymend = isymbuf + locsymcount;
9762 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9764 isym++, pindex++, ppsection++)
9768 Elf_Internal_Sym osym;
9774 if (elf_bad_symtab (input_bfd))
9776 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9783 if (isym->st_shndx == SHN_UNDEF)
9784 isec = bfd_und_section_ptr;
9785 else if (isym->st_shndx == SHN_ABS)
9786 isec = bfd_abs_section_ptr;
9787 else if (isym->st_shndx == SHN_COMMON)
9788 isec = bfd_com_section_ptr;
9791 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9794 /* Don't attempt to output symbols with st_shnx in the
9795 reserved range other than SHN_ABS and SHN_COMMON. */
9799 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9800 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9802 _bfd_merged_section_offset (output_bfd, &isec,
9803 elf_section_data (isec)->sec_info,
9809 /* Don't output the first, undefined, symbol. In fact, don't
9810 output any undefined local symbol. */
9811 if (isec == bfd_und_section_ptr)
9814 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9816 /* We never output section symbols. Instead, we use the
9817 section symbol of the corresponding section in the output
9822 /* If we are stripping all symbols, we don't want to output this
9824 if (flinfo->info->strip == strip_all)
9827 /* If we are discarding all local symbols, we don't want to
9828 output this one. If we are generating a relocatable output
9829 file, then some of the local symbols may be required by
9830 relocs; we output them below as we discover that they are
9832 if (flinfo->info->discard == discard_all)
9835 /* If this symbol is defined in a section which we are
9836 discarding, we don't need to keep it. */
9837 if (isym->st_shndx != SHN_UNDEF
9838 && isym->st_shndx < SHN_LORESERVE
9839 && bfd_section_removed_from_list (output_bfd,
9840 isec->output_section))
9843 /* Get the name of the symbol. */
9844 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9849 /* See if we are discarding symbols with this name. */
9850 if ((flinfo->info->strip == strip_some
9851 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9853 || (((flinfo->info->discard == discard_sec_merge
9854 && (isec->flags & SEC_MERGE)
9855 && !bfd_link_relocatable (flinfo->info))
9856 || flinfo->info->discard == discard_l)
9857 && bfd_is_local_label_name (input_bfd, name)))
9860 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9862 if (input_bfd->lto_output)
9863 /* -flto puts a temp file name here. This means builds
9864 are not reproducible. Discard the symbol. */
9866 have_file_sym = TRUE;
9867 flinfo->filesym_count += 1;
9871 /* In the absence of debug info, bfd_find_nearest_line uses
9872 FILE symbols to determine the source file for local
9873 function symbols. Provide a FILE symbol here if input
9874 files lack such, so that their symbols won't be
9875 associated with a previous input file. It's not the
9876 source file, but the best we can do. */
9877 have_file_sym = TRUE;
9878 flinfo->filesym_count += 1;
9879 memset (&osym, 0, sizeof (osym));
9880 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9881 osym.st_shndx = SHN_ABS;
9882 if (!elf_link_output_symstrtab (flinfo,
9883 (input_bfd->lto_output ? NULL
9884 : input_bfd->filename),
9885 &osym, bfd_abs_section_ptr,
9892 /* Adjust the section index for the output file. */
9893 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9894 isec->output_section);
9895 if (osym.st_shndx == SHN_BAD)
9898 /* ELF symbols in relocatable files are section relative, but
9899 in executable files they are virtual addresses. Note that
9900 this code assumes that all ELF sections have an associated
9901 BFD section with a reasonable value for output_offset; below
9902 we assume that they also have a reasonable value for
9903 output_section. Any special sections must be set up to meet
9904 these requirements. */
9905 osym.st_value += isec->output_offset;
9906 if (!bfd_link_relocatable (flinfo->info))
9908 osym.st_value += isec->output_section->vma;
9909 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9911 /* STT_TLS symbols are relative to PT_TLS segment base. */
9912 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9913 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9917 indx = bfd_get_symcount (output_bfd);
9918 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9925 if (bed->s->arch_size == 32)
9933 r_type_mask = 0xffffffff;
9938 /* Relocate the contents of each section. */
9939 sym_hashes = elf_sym_hashes (input_bfd);
9940 for (o = input_bfd->sections; o != NULL; o = o->next)
9944 if (! o->linker_mark)
9946 /* This section was omitted from the link. */
9950 if (bfd_link_relocatable (flinfo->info)
9951 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9953 /* Deal with the group signature symbol. */
9954 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9955 unsigned long symndx = sec_data->this_hdr.sh_info;
9956 asection *osec = o->output_section;
9958 if (symndx >= locsymcount
9959 || (elf_bad_symtab (input_bfd)
9960 && flinfo->sections[symndx] == NULL))
9962 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9963 while (h->root.type == bfd_link_hash_indirect
9964 || h->root.type == bfd_link_hash_warning)
9965 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9966 /* Arrange for symbol to be output. */
9968 elf_section_data (osec)->this_hdr.sh_info = -2;
9970 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9972 /* We'll use the output section target_index. */
9973 asection *sec = flinfo->sections[symndx]->output_section;
9974 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9978 if (flinfo->indices[symndx] == -1)
9980 /* Otherwise output the local symbol now. */
9981 Elf_Internal_Sym sym = isymbuf[symndx];
9982 asection *sec = flinfo->sections[symndx]->output_section;
9987 name = bfd_elf_string_from_elf_section (input_bfd,
9988 symtab_hdr->sh_link,
9993 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9995 if (sym.st_shndx == SHN_BAD)
9998 sym.st_value += o->output_offset;
10000 indx = bfd_get_symcount (output_bfd);
10001 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10006 flinfo->indices[symndx] = indx;
10010 elf_section_data (osec)->this_hdr.sh_info
10011 = flinfo->indices[symndx];
10015 if ((o->flags & SEC_HAS_CONTENTS) == 0
10016 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10019 if ((o->flags & SEC_LINKER_CREATED) != 0)
10021 /* Section was created by _bfd_elf_link_create_dynamic_sections
10026 /* Get the contents of the section. They have been cached by a
10027 relaxation routine. Note that o is a section in an input
10028 file, so the contents field will not have been set by any of
10029 the routines which work on output files. */
10030 if (elf_section_data (o)->this_hdr.contents != NULL)
10032 contents = elf_section_data (o)->this_hdr.contents;
10033 if (bed->caches_rawsize
10035 && o->rawsize < o->size)
10037 memcpy (flinfo->contents, contents, o->rawsize);
10038 contents = flinfo->contents;
10043 contents = flinfo->contents;
10044 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10048 if ((o->flags & SEC_RELOC) != 0)
10050 Elf_Internal_Rela *internal_relocs;
10051 Elf_Internal_Rela *rel, *relend;
10052 int action_discarded;
10055 /* Get the swapped relocs. */
10057 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10058 flinfo->internal_relocs, FALSE);
10059 if (internal_relocs == NULL
10060 && o->reloc_count > 0)
10063 /* We need to reverse-copy input .ctors/.dtors sections if
10064 they are placed in .init_array/.finit_array for output. */
10065 if (o->size > address_size
10066 && ((strncmp (o->name, ".ctors", 6) == 0
10067 && strcmp (o->output_section->name,
10068 ".init_array") == 0)
10069 || (strncmp (o->name, ".dtors", 6) == 0
10070 && strcmp (o->output_section->name,
10071 ".fini_array") == 0))
10072 && (o->name[6] == 0 || o->name[6] == '.'))
10074 if (o->size != o->reloc_count * address_size)
10076 (*_bfd_error_handler)
10077 (_("error: %B: size of section %A is not "
10078 "multiple of address size"),
10080 bfd_set_error (bfd_error_on_input);
10083 o->flags |= SEC_ELF_REVERSE_COPY;
10086 action_discarded = -1;
10087 if (!elf_section_ignore_discarded_relocs (o))
10088 action_discarded = (*bed->action_discarded) (o);
10090 /* Run through the relocs evaluating complex reloc symbols and
10091 looking for relocs against symbols from discarded sections
10092 or section symbols from removed link-once sections.
10093 Complain about relocs against discarded sections. Zero
10094 relocs against removed link-once sections. */
10096 rel = internal_relocs;
10097 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10098 for ( ; rel < relend; rel++)
10100 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10101 unsigned int s_type;
10102 asection **ps, *sec;
10103 struct elf_link_hash_entry *h = NULL;
10104 const char *sym_name;
10106 if (r_symndx == STN_UNDEF)
10109 if (r_symndx >= locsymcount
10110 || (elf_bad_symtab (input_bfd)
10111 && flinfo->sections[r_symndx] == NULL))
10113 h = sym_hashes[r_symndx - extsymoff];
10115 /* Badly formatted input files can contain relocs that
10116 reference non-existant symbols. Check here so that
10117 we do not seg fault. */
10122 sprintf_vma (buffer, rel->r_info);
10123 (*_bfd_error_handler)
10124 (_("error: %B contains a reloc (0x%s) for section %A "
10125 "that references a non-existent global symbol"),
10126 input_bfd, o, buffer);
10127 bfd_set_error (bfd_error_bad_value);
10131 while (h->root.type == bfd_link_hash_indirect
10132 || h->root.type == bfd_link_hash_warning)
10133 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10137 /* If a plugin symbol is referenced from a non-IR file,
10138 mark the symbol as undefined. Note that the
10139 linker may attach linker created dynamic sections
10140 to the plugin bfd. Symbols defined in linker
10141 created sections are not plugin symbols. */
10142 if (h->root.non_ir_ref
10143 && (h->root.type == bfd_link_hash_defined
10144 || h->root.type == bfd_link_hash_defweak)
10145 && (h->root.u.def.section->flags
10146 & SEC_LINKER_CREATED) == 0
10147 && h->root.u.def.section->owner != NULL
10148 && (h->root.u.def.section->owner->flags
10149 & BFD_PLUGIN) != 0)
10151 h->root.type = bfd_link_hash_undefined;
10152 h->root.u.undef.abfd = h->root.u.def.section->owner;
10156 if (h->root.type == bfd_link_hash_defined
10157 || h->root.type == bfd_link_hash_defweak)
10158 ps = &h->root.u.def.section;
10160 sym_name = h->root.root.string;
10164 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10166 s_type = ELF_ST_TYPE (sym->st_info);
10167 ps = &flinfo->sections[r_symndx];
10168 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10172 if ((s_type == STT_RELC || s_type == STT_SRELC)
10173 && !bfd_link_relocatable (flinfo->info))
10176 bfd_vma dot = (rel->r_offset
10177 + o->output_offset + o->output_section->vma);
10179 printf ("Encountered a complex symbol!");
10180 printf (" (input_bfd %s, section %s, reloc %ld\n",
10181 input_bfd->filename, o->name,
10182 (long) (rel - internal_relocs));
10183 printf (" symbol: idx %8.8lx, name %s\n",
10184 r_symndx, sym_name);
10185 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10186 (unsigned long) rel->r_info,
10187 (unsigned long) rel->r_offset);
10189 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10190 isymbuf, locsymcount, s_type == STT_SRELC))
10193 /* Symbol evaluated OK. Update to absolute value. */
10194 set_symbol_value (input_bfd, isymbuf, locsymcount,
10199 if (action_discarded != -1 && ps != NULL)
10201 /* Complain if the definition comes from a
10202 discarded section. */
10203 if ((sec = *ps) != NULL && discarded_section (sec))
10205 BFD_ASSERT (r_symndx != STN_UNDEF);
10206 if (action_discarded & COMPLAIN)
10207 (*flinfo->info->callbacks->einfo)
10208 (_("%X`%s' referenced in section `%A' of %B: "
10209 "defined in discarded section `%A' of %B\n"),
10210 sym_name, o, input_bfd, sec, sec->owner);
10212 /* Try to do the best we can to support buggy old
10213 versions of gcc. Pretend that the symbol is
10214 really defined in the kept linkonce section.
10215 FIXME: This is quite broken. Modifying the
10216 symbol here means we will be changing all later
10217 uses of the symbol, not just in this section. */
10218 if (action_discarded & PRETEND)
10222 kept = _bfd_elf_check_kept_section (sec,
10234 /* Relocate the section by invoking a back end routine.
10236 The back end routine is responsible for adjusting the
10237 section contents as necessary, and (if using Rela relocs
10238 and generating a relocatable output file) adjusting the
10239 reloc addend as necessary.
10241 The back end routine does not have to worry about setting
10242 the reloc address or the reloc symbol index.
10244 The back end routine is given a pointer to the swapped in
10245 internal symbols, and can access the hash table entries
10246 for the external symbols via elf_sym_hashes (input_bfd).
10248 When generating relocatable output, the back end routine
10249 must handle STB_LOCAL/STT_SECTION symbols specially. The
10250 output symbol is going to be a section symbol
10251 corresponding to the output section, which will require
10252 the addend to be adjusted. */
10254 ret = (*relocate_section) (output_bfd, flinfo->info,
10255 input_bfd, o, contents,
10263 || bfd_link_relocatable (flinfo->info)
10264 || flinfo->info->emitrelocations)
10266 Elf_Internal_Rela *irela;
10267 Elf_Internal_Rela *irelaend, *irelamid;
10268 bfd_vma last_offset;
10269 struct elf_link_hash_entry **rel_hash;
10270 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10271 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10272 unsigned int next_erel;
10273 bfd_boolean rela_normal;
10274 struct bfd_elf_section_data *esdi, *esdo;
10276 esdi = elf_section_data (o);
10277 esdo = elf_section_data (o->output_section);
10278 rela_normal = FALSE;
10280 /* Adjust the reloc addresses and symbol indices. */
10282 irela = internal_relocs;
10283 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10284 rel_hash = esdo->rel.hashes + esdo->rel.count;
10285 /* We start processing the REL relocs, if any. When we reach
10286 IRELAMID in the loop, we switch to the RELA relocs. */
10288 if (esdi->rel.hdr != NULL)
10289 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10290 * bed->s->int_rels_per_ext_rel);
10291 rel_hash_list = rel_hash;
10292 rela_hash_list = NULL;
10293 last_offset = o->output_offset;
10294 if (!bfd_link_relocatable (flinfo->info))
10295 last_offset += o->output_section->vma;
10296 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10298 unsigned long r_symndx;
10300 Elf_Internal_Sym sym;
10302 if (next_erel == bed->s->int_rels_per_ext_rel)
10308 if (irela == irelamid)
10310 rel_hash = esdo->rela.hashes + esdo->rela.count;
10311 rela_hash_list = rel_hash;
10312 rela_normal = bed->rela_normal;
10315 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10318 if (irela->r_offset >= (bfd_vma) -2)
10320 /* This is a reloc for a deleted entry or somesuch.
10321 Turn it into an R_*_NONE reloc, at the same
10322 offset as the last reloc. elf_eh_frame.c and
10323 bfd_elf_discard_info rely on reloc offsets
10325 irela->r_offset = last_offset;
10327 irela->r_addend = 0;
10331 irela->r_offset += o->output_offset;
10333 /* Relocs in an executable have to be virtual addresses. */
10334 if (!bfd_link_relocatable (flinfo->info))
10335 irela->r_offset += o->output_section->vma;
10337 last_offset = irela->r_offset;
10339 r_symndx = irela->r_info >> r_sym_shift;
10340 if (r_symndx == STN_UNDEF)
10343 if (r_symndx >= locsymcount
10344 || (elf_bad_symtab (input_bfd)
10345 && flinfo->sections[r_symndx] == NULL))
10347 struct elf_link_hash_entry *rh;
10348 unsigned long indx;
10350 /* This is a reloc against a global symbol. We
10351 have not yet output all the local symbols, so
10352 we do not know the symbol index of any global
10353 symbol. We set the rel_hash entry for this
10354 reloc to point to the global hash table entry
10355 for this symbol. The symbol index is then
10356 set at the end of bfd_elf_final_link. */
10357 indx = r_symndx - extsymoff;
10358 rh = elf_sym_hashes (input_bfd)[indx];
10359 while (rh->root.type == bfd_link_hash_indirect
10360 || rh->root.type == bfd_link_hash_warning)
10361 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10363 /* Setting the index to -2 tells
10364 elf_link_output_extsym that this symbol is
10365 used by a reloc. */
10366 BFD_ASSERT (rh->indx < 0);
10374 /* This is a reloc against a local symbol. */
10377 sym = isymbuf[r_symndx];
10378 sec = flinfo->sections[r_symndx];
10379 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10381 /* I suppose the backend ought to fill in the
10382 section of any STT_SECTION symbol against a
10383 processor specific section. */
10384 r_symndx = STN_UNDEF;
10385 if (bfd_is_abs_section (sec))
10387 else if (sec == NULL || sec->owner == NULL)
10389 bfd_set_error (bfd_error_bad_value);
10394 asection *osec = sec->output_section;
10396 /* If we have discarded a section, the output
10397 section will be the absolute section. In
10398 case of discarded SEC_MERGE sections, use
10399 the kept section. relocate_section should
10400 have already handled discarded linkonce
10402 if (bfd_is_abs_section (osec)
10403 && sec->kept_section != NULL
10404 && sec->kept_section->output_section != NULL)
10406 osec = sec->kept_section->output_section;
10407 irela->r_addend -= osec->vma;
10410 if (!bfd_is_abs_section (osec))
10412 r_symndx = osec->target_index;
10413 if (r_symndx == STN_UNDEF)
10415 irela->r_addend += osec->vma;
10416 osec = _bfd_nearby_section (output_bfd, osec,
10418 irela->r_addend -= osec->vma;
10419 r_symndx = osec->target_index;
10424 /* Adjust the addend according to where the
10425 section winds up in the output section. */
10427 irela->r_addend += sec->output_offset;
10431 if (flinfo->indices[r_symndx] == -1)
10433 unsigned long shlink;
10438 if (flinfo->info->strip == strip_all)
10440 /* You can't do ld -r -s. */
10441 bfd_set_error (bfd_error_invalid_operation);
10445 /* This symbol was skipped earlier, but
10446 since it is needed by a reloc, we
10447 must output it now. */
10448 shlink = symtab_hdr->sh_link;
10449 name = (bfd_elf_string_from_elf_section
10450 (input_bfd, shlink, sym.st_name));
10454 osec = sec->output_section;
10456 _bfd_elf_section_from_bfd_section (output_bfd,
10458 if (sym.st_shndx == SHN_BAD)
10461 sym.st_value += sec->output_offset;
10462 if (!bfd_link_relocatable (flinfo->info))
10464 sym.st_value += osec->vma;
10465 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10467 /* STT_TLS symbols are relative to PT_TLS
10469 BFD_ASSERT (elf_hash_table (flinfo->info)
10470 ->tls_sec != NULL);
10471 sym.st_value -= (elf_hash_table (flinfo->info)
10476 indx = bfd_get_symcount (output_bfd);
10477 ret = elf_link_output_symstrtab (flinfo, name,
10483 flinfo->indices[r_symndx] = indx;
10488 r_symndx = flinfo->indices[r_symndx];
10491 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10492 | (irela->r_info & r_type_mask));
10495 /* Swap out the relocs. */
10496 input_rel_hdr = esdi->rel.hdr;
10497 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10499 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10504 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10505 * bed->s->int_rels_per_ext_rel);
10506 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10509 input_rela_hdr = esdi->rela.hdr;
10510 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10512 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10521 /* Write out the modified section contents. */
10522 if (bed->elf_backend_write_section
10523 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10526 /* Section written out. */
10528 else switch (o->sec_info_type)
10530 case SEC_INFO_TYPE_STABS:
10531 if (! (_bfd_write_section_stabs
10533 &elf_hash_table (flinfo->info)->stab_info,
10534 o, &elf_section_data (o)->sec_info, contents)))
10537 case SEC_INFO_TYPE_MERGE:
10538 if (! _bfd_write_merged_section (output_bfd, o,
10539 elf_section_data (o)->sec_info))
10542 case SEC_INFO_TYPE_EH_FRAME:
10544 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10549 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10551 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10559 if (! (o->flags & SEC_EXCLUDE))
10561 file_ptr offset = (file_ptr) o->output_offset;
10562 bfd_size_type todo = o->size;
10564 offset *= bfd_octets_per_byte (output_bfd);
10566 if ((o->flags & SEC_ELF_REVERSE_COPY))
10568 /* Reverse-copy input section to output. */
10571 todo -= address_size;
10572 if (! bfd_set_section_contents (output_bfd,
10580 offset += address_size;
10584 else if (! bfd_set_section_contents (output_bfd,
10598 /* Generate a reloc when linking an ELF file. This is a reloc
10599 requested by the linker, and does not come from any input file. This
10600 is used to build constructor and destructor tables when linking
10604 elf_reloc_link_order (bfd *output_bfd,
10605 struct bfd_link_info *info,
10606 asection *output_section,
10607 struct bfd_link_order *link_order)
10609 reloc_howto_type *howto;
10613 struct bfd_elf_section_reloc_data *reldata;
10614 struct elf_link_hash_entry **rel_hash_ptr;
10615 Elf_Internal_Shdr *rel_hdr;
10616 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10617 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10620 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10622 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10625 bfd_set_error (bfd_error_bad_value);
10629 addend = link_order->u.reloc.p->addend;
10632 reldata = &esdo->rel;
10633 else if (esdo->rela.hdr)
10634 reldata = &esdo->rela;
10641 /* Figure out the symbol index. */
10642 rel_hash_ptr = reldata->hashes + reldata->count;
10643 if (link_order->type == bfd_section_reloc_link_order)
10645 indx = link_order->u.reloc.p->u.section->target_index;
10646 BFD_ASSERT (indx != 0);
10647 *rel_hash_ptr = NULL;
10651 struct elf_link_hash_entry *h;
10653 /* Treat a reloc against a defined symbol as though it were
10654 actually against the section. */
10655 h = ((struct elf_link_hash_entry *)
10656 bfd_wrapped_link_hash_lookup (output_bfd, info,
10657 link_order->u.reloc.p->u.name,
10658 FALSE, FALSE, TRUE));
10660 && (h->root.type == bfd_link_hash_defined
10661 || h->root.type == bfd_link_hash_defweak))
10665 section = h->root.u.def.section;
10666 indx = section->output_section->target_index;
10667 *rel_hash_ptr = NULL;
10668 /* It seems that we ought to add the symbol value to the
10669 addend here, but in practice it has already been added
10670 because it was passed to constructor_callback. */
10671 addend += section->output_section->vma + section->output_offset;
10673 else if (h != NULL)
10675 /* Setting the index to -2 tells elf_link_output_extsym that
10676 this symbol is used by a reloc. */
10683 if (! ((*info->callbacks->unattached_reloc)
10684 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10690 /* If this is an inplace reloc, we must write the addend into the
10692 if (howto->partial_inplace && addend != 0)
10694 bfd_size_type size;
10695 bfd_reloc_status_type rstat;
10698 const char *sym_name;
10700 size = (bfd_size_type) bfd_get_reloc_size (howto);
10701 buf = (bfd_byte *) bfd_zmalloc (size);
10702 if (buf == NULL && size != 0)
10704 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10711 case bfd_reloc_outofrange:
10714 case bfd_reloc_overflow:
10715 if (link_order->type == bfd_section_reloc_link_order)
10716 sym_name = bfd_section_name (output_bfd,
10717 link_order->u.reloc.p->u.section);
10719 sym_name = link_order->u.reloc.p->u.name;
10720 if (! ((*info->callbacks->reloc_overflow)
10721 (info, NULL, sym_name, howto->name, addend, NULL,
10722 NULL, (bfd_vma) 0)))
10730 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10732 * bfd_octets_per_byte (output_bfd),
10739 /* The address of a reloc is relative to the section in a
10740 relocatable file, and is a virtual address in an executable
10742 offset = link_order->offset;
10743 if (! bfd_link_relocatable (info))
10744 offset += output_section->vma;
10746 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10748 irel[i].r_offset = offset;
10749 irel[i].r_info = 0;
10750 irel[i].r_addend = 0;
10752 if (bed->s->arch_size == 32)
10753 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10755 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10757 rel_hdr = reldata->hdr;
10758 erel = rel_hdr->contents;
10759 if (rel_hdr->sh_type == SHT_REL)
10761 erel += reldata->count * bed->s->sizeof_rel;
10762 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10766 irel[0].r_addend = addend;
10767 erel += reldata->count * bed->s->sizeof_rela;
10768 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10777 /* Get the output vma of the section pointed to by the sh_link field. */
10780 elf_get_linked_section_vma (struct bfd_link_order *p)
10782 Elf_Internal_Shdr **elf_shdrp;
10786 s = p->u.indirect.section;
10787 elf_shdrp = elf_elfsections (s->owner);
10788 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10789 elfsec = elf_shdrp[elfsec]->sh_link;
10791 The Intel C compiler generates SHT_IA_64_UNWIND with
10792 SHF_LINK_ORDER. But it doesn't set the sh_link or
10793 sh_info fields. Hence we could get the situation
10794 where elfsec is 0. */
10797 const struct elf_backend_data *bed
10798 = get_elf_backend_data (s->owner);
10799 if (bed->link_order_error_handler)
10800 bed->link_order_error_handler
10801 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10806 s = elf_shdrp[elfsec]->bfd_section;
10807 return s->output_section->vma + s->output_offset;
10812 /* Compare two sections based on the locations of the sections they are
10813 linked to. Used by elf_fixup_link_order. */
10816 compare_link_order (const void * a, const void * b)
10821 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10822 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10825 return apos > bpos;
10829 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10830 order as their linked sections. Returns false if this could not be done
10831 because an output section includes both ordered and unordered
10832 sections. Ideally we'd do this in the linker proper. */
10835 elf_fixup_link_order (bfd *abfd, asection *o)
10837 int seen_linkorder;
10840 struct bfd_link_order *p;
10842 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10844 struct bfd_link_order **sections;
10845 asection *s, *other_sec, *linkorder_sec;
10849 linkorder_sec = NULL;
10851 seen_linkorder = 0;
10852 for (p = o->map_head.link_order; p != NULL; p = p->next)
10854 if (p->type == bfd_indirect_link_order)
10856 s = p->u.indirect.section;
10858 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10859 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10860 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10861 && elfsec < elf_numsections (sub)
10862 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10863 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10877 if (seen_other && seen_linkorder)
10879 if (other_sec && linkorder_sec)
10880 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10882 linkorder_sec->owner, other_sec,
10885 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10887 bfd_set_error (bfd_error_bad_value);
10892 if (!seen_linkorder)
10895 sections = (struct bfd_link_order **)
10896 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10897 if (sections == NULL)
10899 seen_linkorder = 0;
10901 for (p = o->map_head.link_order; p != NULL; p = p->next)
10903 sections[seen_linkorder++] = p;
10905 /* Sort the input sections in the order of their linked section. */
10906 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10907 compare_link_order);
10909 /* Change the offsets of the sections. */
10911 for (n = 0; n < seen_linkorder; n++)
10913 s = sections[n]->u.indirect.section;
10914 offset &= ~(bfd_vma) 0 << s->alignment_power;
10915 s->output_offset = offset / bfd_octets_per_byte (abfd);
10916 sections[n]->offset = offset;
10917 offset += sections[n]->size;
10925 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10929 if (flinfo->symstrtab != NULL)
10930 _bfd_elf_strtab_free (flinfo->symstrtab);
10931 if (flinfo->contents != NULL)
10932 free (flinfo->contents);
10933 if (flinfo->external_relocs != NULL)
10934 free (flinfo->external_relocs);
10935 if (flinfo->internal_relocs != NULL)
10936 free (flinfo->internal_relocs);
10937 if (flinfo->external_syms != NULL)
10938 free (flinfo->external_syms);
10939 if (flinfo->locsym_shndx != NULL)
10940 free (flinfo->locsym_shndx);
10941 if (flinfo->internal_syms != NULL)
10942 free (flinfo->internal_syms);
10943 if (flinfo->indices != NULL)
10944 free (flinfo->indices);
10945 if (flinfo->sections != NULL)
10946 free (flinfo->sections);
10947 if (flinfo->symshndxbuf != NULL)
10948 free (flinfo->symshndxbuf);
10949 for (o = obfd->sections; o != NULL; o = o->next)
10951 struct bfd_elf_section_data *esdo = elf_section_data (o);
10952 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10953 free (esdo->rel.hashes);
10954 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10955 free (esdo->rela.hashes);
10959 /* Do the final step of an ELF link. */
10962 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10964 bfd_boolean dynamic;
10965 bfd_boolean emit_relocs;
10967 struct elf_final_link_info flinfo;
10969 struct bfd_link_order *p;
10971 bfd_size_type max_contents_size;
10972 bfd_size_type max_external_reloc_size;
10973 bfd_size_type max_internal_reloc_count;
10974 bfd_size_type max_sym_count;
10975 bfd_size_type max_sym_shndx_count;
10976 Elf_Internal_Sym elfsym;
10978 Elf_Internal_Shdr *symtab_hdr;
10979 Elf_Internal_Shdr *symtab_shndx_hdr;
10980 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10981 struct elf_outext_info eoinfo;
10982 bfd_boolean merged;
10983 size_t relativecount = 0;
10984 asection *reldyn = 0;
10986 asection *attr_section = NULL;
10987 bfd_vma attr_size = 0;
10988 const char *std_attrs_section;
10990 if (! is_elf_hash_table (info->hash))
10993 if (bfd_link_pic (info))
10994 abfd->flags |= DYNAMIC;
10996 dynamic = elf_hash_table (info)->dynamic_sections_created;
10997 dynobj = elf_hash_table (info)->dynobj;
10999 emit_relocs = (bfd_link_relocatable (info)
11000 || info->emitrelocations);
11002 flinfo.info = info;
11003 flinfo.output_bfd = abfd;
11004 flinfo.symstrtab = _bfd_elf_strtab_init ();
11005 if (flinfo.symstrtab == NULL)
11010 flinfo.hash_sec = NULL;
11011 flinfo.symver_sec = NULL;
11015 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11016 /* Note that dynsym_sec can be NULL (on VMS). */
11017 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11018 /* Note that it is OK if symver_sec is NULL. */
11021 flinfo.contents = NULL;
11022 flinfo.external_relocs = NULL;
11023 flinfo.internal_relocs = NULL;
11024 flinfo.external_syms = NULL;
11025 flinfo.locsym_shndx = NULL;
11026 flinfo.internal_syms = NULL;
11027 flinfo.indices = NULL;
11028 flinfo.sections = NULL;
11029 flinfo.symshndxbuf = NULL;
11030 flinfo.filesym_count = 0;
11032 /* The object attributes have been merged. Remove the input
11033 sections from the link, and set the contents of the output
11035 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11036 for (o = abfd->sections; o != NULL; o = o->next)
11038 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11039 || strcmp (o->name, ".gnu.attributes") == 0)
11041 for (p = o->map_head.link_order; p != NULL; p = p->next)
11043 asection *input_section;
11045 if (p->type != bfd_indirect_link_order)
11047 input_section = p->u.indirect.section;
11048 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11049 elf_link_input_bfd ignores this section. */
11050 input_section->flags &= ~SEC_HAS_CONTENTS;
11053 attr_size = bfd_elf_obj_attr_size (abfd);
11056 bfd_set_section_size (abfd, o, attr_size);
11058 /* Skip this section later on. */
11059 o->map_head.link_order = NULL;
11062 o->flags |= SEC_EXCLUDE;
11066 /* Count up the number of relocations we will output for each output
11067 section, so that we know the sizes of the reloc sections. We
11068 also figure out some maximum sizes. */
11069 max_contents_size = 0;
11070 max_external_reloc_size = 0;
11071 max_internal_reloc_count = 0;
11073 max_sym_shndx_count = 0;
11075 for (o = abfd->sections; o != NULL; o = o->next)
11077 struct bfd_elf_section_data *esdo = elf_section_data (o);
11078 o->reloc_count = 0;
11080 for (p = o->map_head.link_order; p != NULL; p = p->next)
11082 unsigned int reloc_count = 0;
11083 unsigned int additional_reloc_count = 0;
11084 struct bfd_elf_section_data *esdi = NULL;
11086 if (p->type == bfd_section_reloc_link_order
11087 || p->type == bfd_symbol_reloc_link_order)
11089 else if (p->type == bfd_indirect_link_order)
11093 sec = p->u.indirect.section;
11094 esdi = elf_section_data (sec);
11096 /* Mark all sections which are to be included in the
11097 link. This will normally be every section. We need
11098 to do this so that we can identify any sections which
11099 the linker has decided to not include. */
11100 sec->linker_mark = TRUE;
11102 if (sec->flags & SEC_MERGE)
11105 if (esdo->this_hdr.sh_type == SHT_REL
11106 || esdo->this_hdr.sh_type == SHT_RELA)
11107 /* Some backends use reloc_count in relocation sections
11108 to count particular types of relocs. Of course,
11109 reloc sections themselves can't have relocations. */
11111 else if (emit_relocs)
11113 reloc_count = sec->reloc_count;
11114 if (bed->elf_backend_count_additional_relocs)
11117 c = (*bed->elf_backend_count_additional_relocs) (sec);
11118 additional_reloc_count += c;
11121 else if (bed->elf_backend_count_relocs)
11122 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11124 if (sec->rawsize > max_contents_size)
11125 max_contents_size = sec->rawsize;
11126 if (sec->size > max_contents_size)
11127 max_contents_size = sec->size;
11129 /* We are interested in just local symbols, not all
11131 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11132 && (sec->owner->flags & DYNAMIC) == 0)
11136 if (elf_bad_symtab (sec->owner))
11137 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11138 / bed->s->sizeof_sym);
11140 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11142 if (sym_count > max_sym_count)
11143 max_sym_count = sym_count;
11145 if (sym_count > max_sym_shndx_count
11146 && elf_symtab_shndx_list (sec->owner) != NULL)
11147 max_sym_shndx_count = sym_count;
11149 if ((sec->flags & SEC_RELOC) != 0)
11151 size_t ext_size = 0;
11153 if (esdi->rel.hdr != NULL)
11154 ext_size = esdi->rel.hdr->sh_size;
11155 if (esdi->rela.hdr != NULL)
11156 ext_size += esdi->rela.hdr->sh_size;
11158 if (ext_size > max_external_reloc_size)
11159 max_external_reloc_size = ext_size;
11160 if (sec->reloc_count > max_internal_reloc_count)
11161 max_internal_reloc_count = sec->reloc_count;
11166 if (reloc_count == 0)
11169 reloc_count += additional_reloc_count;
11170 o->reloc_count += reloc_count;
11172 if (p->type == bfd_indirect_link_order && emit_relocs)
11176 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11177 esdo->rel.count += additional_reloc_count;
11179 if (esdi->rela.hdr)
11181 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11182 esdo->rela.count += additional_reloc_count;
11188 esdo->rela.count += reloc_count;
11190 esdo->rel.count += reloc_count;
11194 if (o->reloc_count > 0)
11195 o->flags |= SEC_RELOC;
11198 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11199 set it (this is probably a bug) and if it is set
11200 assign_section_numbers will create a reloc section. */
11201 o->flags &=~ SEC_RELOC;
11204 /* If the SEC_ALLOC flag is not set, force the section VMA to
11205 zero. This is done in elf_fake_sections as well, but forcing
11206 the VMA to 0 here will ensure that relocs against these
11207 sections are handled correctly. */
11208 if ((o->flags & SEC_ALLOC) == 0
11209 && ! o->user_set_vma)
11213 if (! bfd_link_relocatable (info) && merged)
11214 elf_link_hash_traverse (elf_hash_table (info),
11215 _bfd_elf_link_sec_merge_syms, abfd);
11217 /* Figure out the file positions for everything but the symbol table
11218 and the relocs. We set symcount to force assign_section_numbers
11219 to create a symbol table. */
11220 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11221 BFD_ASSERT (! abfd->output_has_begun);
11222 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11225 /* Set sizes, and assign file positions for reloc sections. */
11226 for (o = abfd->sections; o != NULL; o = o->next)
11228 struct bfd_elf_section_data *esdo = elf_section_data (o);
11229 if ((o->flags & SEC_RELOC) != 0)
11232 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11236 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11240 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11241 to count upwards while actually outputting the relocations. */
11242 esdo->rel.count = 0;
11243 esdo->rela.count = 0;
11245 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11247 /* Cache the section contents so that they can be compressed
11248 later. Use bfd_malloc since it will be freed by
11249 bfd_compress_section_contents. */
11250 unsigned char *contents = esdo->this_hdr.contents;
11251 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11254 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11255 if (contents == NULL)
11257 esdo->this_hdr.contents = contents;
11261 /* We have now assigned file positions for all the sections except
11262 .symtab, .strtab, and non-loaded reloc sections. We start the
11263 .symtab section at the current file position, and write directly
11264 to it. We build the .strtab section in memory. */
11265 bfd_get_symcount (abfd) = 0;
11266 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11267 /* sh_name is set in prep_headers. */
11268 symtab_hdr->sh_type = SHT_SYMTAB;
11269 /* sh_flags, sh_addr and sh_size all start off zero. */
11270 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11271 /* sh_link is set in assign_section_numbers. */
11272 /* sh_info is set below. */
11273 /* sh_offset is set just below. */
11274 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11276 if (max_sym_count < 20)
11277 max_sym_count = 20;
11278 elf_hash_table (info)->strtabsize = max_sym_count;
11279 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11280 elf_hash_table (info)->strtab
11281 = (struct elf_sym_strtab *) bfd_malloc (amt);
11282 if (elf_hash_table (info)->strtab == NULL)
11284 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11286 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11287 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11289 if (info->strip != strip_all || emit_relocs)
11291 file_ptr off = elf_next_file_pos (abfd);
11293 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11295 /* Note that at this point elf_next_file_pos (abfd) is
11296 incorrect. We do not yet know the size of the .symtab section.
11297 We correct next_file_pos below, after we do know the size. */
11299 /* Start writing out the symbol table. The first symbol is always a
11301 elfsym.st_value = 0;
11302 elfsym.st_size = 0;
11303 elfsym.st_info = 0;
11304 elfsym.st_other = 0;
11305 elfsym.st_shndx = SHN_UNDEF;
11306 elfsym.st_target_internal = 0;
11307 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11308 bfd_und_section_ptr, NULL) != 1)
11311 /* Output a symbol for each section. We output these even if we are
11312 discarding local symbols, since they are used for relocs. These
11313 symbols have no names. We store the index of each one in the
11314 index field of the section, so that we can find it again when
11315 outputting relocs. */
11317 elfsym.st_size = 0;
11318 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11319 elfsym.st_other = 0;
11320 elfsym.st_value = 0;
11321 elfsym.st_target_internal = 0;
11322 for (i = 1; i < elf_numsections (abfd); i++)
11324 o = bfd_section_from_elf_index (abfd, i);
11327 o->target_index = bfd_get_symcount (abfd);
11328 elfsym.st_shndx = i;
11329 if (!bfd_link_relocatable (info))
11330 elfsym.st_value = o->vma;
11331 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11338 /* Allocate some memory to hold information read in from the input
11340 if (max_contents_size != 0)
11342 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11343 if (flinfo.contents == NULL)
11347 if (max_external_reloc_size != 0)
11349 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11350 if (flinfo.external_relocs == NULL)
11354 if (max_internal_reloc_count != 0)
11356 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11357 amt *= sizeof (Elf_Internal_Rela);
11358 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11359 if (flinfo.internal_relocs == NULL)
11363 if (max_sym_count != 0)
11365 amt = max_sym_count * bed->s->sizeof_sym;
11366 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11367 if (flinfo.external_syms == NULL)
11370 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11371 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11372 if (flinfo.internal_syms == NULL)
11375 amt = max_sym_count * sizeof (long);
11376 flinfo.indices = (long int *) bfd_malloc (amt);
11377 if (flinfo.indices == NULL)
11380 amt = max_sym_count * sizeof (asection *);
11381 flinfo.sections = (asection **) bfd_malloc (amt);
11382 if (flinfo.sections == NULL)
11386 if (max_sym_shndx_count != 0)
11388 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11389 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11390 if (flinfo.locsym_shndx == NULL)
11394 if (elf_hash_table (info)->tls_sec)
11396 bfd_vma base, end = 0;
11399 for (sec = elf_hash_table (info)->tls_sec;
11400 sec && (sec->flags & SEC_THREAD_LOCAL);
11403 bfd_size_type size = sec->size;
11406 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11408 struct bfd_link_order *ord = sec->map_tail.link_order;
11411 size = ord->offset + ord->size;
11413 end = sec->vma + size;
11415 base = elf_hash_table (info)->tls_sec->vma;
11416 /* Only align end of TLS section if static TLS doesn't have special
11417 alignment requirements. */
11418 if (bed->static_tls_alignment == 1)
11419 end = align_power (end,
11420 elf_hash_table (info)->tls_sec->alignment_power);
11421 elf_hash_table (info)->tls_size = end - base;
11424 /* Reorder SHF_LINK_ORDER sections. */
11425 for (o = abfd->sections; o != NULL; o = o->next)
11427 if (!elf_fixup_link_order (abfd, o))
11431 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11434 /* Since ELF permits relocations to be against local symbols, we
11435 must have the local symbols available when we do the relocations.
11436 Since we would rather only read the local symbols once, and we
11437 would rather not keep them in memory, we handle all the
11438 relocations for a single input file at the same time.
11440 Unfortunately, there is no way to know the total number of local
11441 symbols until we have seen all of them, and the local symbol
11442 indices precede the global symbol indices. This means that when
11443 we are generating relocatable output, and we see a reloc against
11444 a global symbol, we can not know the symbol index until we have
11445 finished examining all the local symbols to see which ones we are
11446 going to output. To deal with this, we keep the relocations in
11447 memory, and don't output them until the end of the link. This is
11448 an unfortunate waste of memory, but I don't see a good way around
11449 it. Fortunately, it only happens when performing a relocatable
11450 link, which is not the common case. FIXME: If keep_memory is set
11451 we could write the relocs out and then read them again; I don't
11452 know how bad the memory loss will be. */
11454 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11455 sub->output_has_begun = FALSE;
11456 for (o = abfd->sections; o != NULL; o = o->next)
11458 for (p = o->map_head.link_order; p != NULL; p = p->next)
11460 if (p->type == bfd_indirect_link_order
11461 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11462 == bfd_target_elf_flavour)
11463 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11465 if (! sub->output_has_begun)
11467 if (! elf_link_input_bfd (&flinfo, sub))
11469 sub->output_has_begun = TRUE;
11472 else if (p->type == bfd_section_reloc_link_order
11473 || p->type == bfd_symbol_reloc_link_order)
11475 if (! elf_reloc_link_order (abfd, info, o, p))
11480 if (! _bfd_default_link_order (abfd, info, o, p))
11482 if (p->type == bfd_indirect_link_order
11483 && (bfd_get_flavour (sub)
11484 == bfd_target_elf_flavour)
11485 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11486 != bed->s->elfclass))
11488 const char *iclass, *oclass;
11490 switch (bed->s->elfclass)
11492 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11493 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11494 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11498 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11500 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11501 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11502 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11506 bfd_set_error (bfd_error_wrong_format);
11507 (*_bfd_error_handler)
11508 (_("%B: file class %s incompatible with %s"),
11509 sub, iclass, oclass);
11518 /* Free symbol buffer if needed. */
11519 if (!info->reduce_memory_overheads)
11521 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11522 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11523 && elf_tdata (sub)->symbuf)
11525 free (elf_tdata (sub)->symbuf);
11526 elf_tdata (sub)->symbuf = NULL;
11530 /* Output any global symbols that got converted to local in a
11531 version script or due to symbol visibility. We do this in a
11532 separate step since ELF requires all local symbols to appear
11533 prior to any global symbols. FIXME: We should only do this if
11534 some global symbols were, in fact, converted to become local.
11535 FIXME: Will this work correctly with the Irix 5 linker? */
11536 eoinfo.failed = FALSE;
11537 eoinfo.flinfo = &flinfo;
11538 eoinfo.localsyms = TRUE;
11539 eoinfo.file_sym_done = FALSE;
11540 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11544 /* If backend needs to output some local symbols not present in the hash
11545 table, do it now. */
11546 if (bed->elf_backend_output_arch_local_syms
11547 && (info->strip != strip_all || emit_relocs))
11549 typedef int (*out_sym_func)
11550 (void *, const char *, Elf_Internal_Sym *, asection *,
11551 struct elf_link_hash_entry *);
11553 if (! ((*bed->elf_backend_output_arch_local_syms)
11554 (abfd, info, &flinfo,
11555 (out_sym_func) elf_link_output_symstrtab)))
11559 /* That wrote out all the local symbols. Finish up the symbol table
11560 with the global symbols. Even if we want to strip everything we
11561 can, we still need to deal with those global symbols that got
11562 converted to local in a version script. */
11564 /* The sh_info field records the index of the first non local symbol. */
11565 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11568 && elf_hash_table (info)->dynsym != NULL
11569 && (elf_hash_table (info)->dynsym->output_section
11570 != bfd_abs_section_ptr))
11572 Elf_Internal_Sym sym;
11573 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11574 long last_local = 0;
11576 /* Write out the section symbols for the output sections. */
11577 if (bfd_link_pic (info)
11578 || elf_hash_table (info)->is_relocatable_executable)
11584 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11586 sym.st_target_internal = 0;
11588 for (s = abfd->sections; s != NULL; s = s->next)
11594 dynindx = elf_section_data (s)->dynindx;
11597 indx = elf_section_data (s)->this_idx;
11598 BFD_ASSERT (indx > 0);
11599 sym.st_shndx = indx;
11600 if (! check_dynsym (abfd, &sym))
11602 sym.st_value = s->vma;
11603 dest = dynsym + dynindx * bed->s->sizeof_sym;
11604 if (last_local < dynindx)
11605 last_local = dynindx;
11606 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11610 /* Write out the local dynsyms. */
11611 if (elf_hash_table (info)->dynlocal)
11613 struct elf_link_local_dynamic_entry *e;
11614 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11619 /* Copy the internal symbol and turn off visibility.
11620 Note that we saved a word of storage and overwrote
11621 the original st_name with the dynstr_index. */
11623 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11625 s = bfd_section_from_elf_index (e->input_bfd,
11630 elf_section_data (s->output_section)->this_idx;
11631 if (! check_dynsym (abfd, &sym))
11633 sym.st_value = (s->output_section->vma
11635 + e->isym.st_value);
11638 if (last_local < e->dynindx)
11639 last_local = e->dynindx;
11641 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11642 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11646 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11650 /* We get the global symbols from the hash table. */
11651 eoinfo.failed = FALSE;
11652 eoinfo.localsyms = FALSE;
11653 eoinfo.flinfo = &flinfo;
11654 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11658 /* If backend needs to output some symbols not present in the hash
11659 table, do it now. */
11660 if (bed->elf_backend_output_arch_syms
11661 && (info->strip != strip_all || emit_relocs))
11663 typedef int (*out_sym_func)
11664 (void *, const char *, Elf_Internal_Sym *, asection *,
11665 struct elf_link_hash_entry *);
11667 if (! ((*bed->elf_backend_output_arch_syms)
11668 (abfd, info, &flinfo,
11669 (out_sym_func) elf_link_output_symstrtab)))
11673 /* Finalize the .strtab section. */
11674 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11676 /* Swap out the .strtab section. */
11677 if (!elf_link_swap_symbols_out (&flinfo))
11680 /* Now we know the size of the symtab section. */
11681 if (bfd_get_symcount (abfd) > 0)
11683 /* Finish up and write out the symbol string table (.strtab)
11685 Elf_Internal_Shdr *symstrtab_hdr;
11686 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11688 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11689 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11691 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11692 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11693 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11694 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11695 symtab_shndx_hdr->sh_size = amt;
11697 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11700 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11701 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11705 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11706 /* sh_name was set in prep_headers. */
11707 symstrtab_hdr->sh_type = SHT_STRTAB;
11708 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
11709 symstrtab_hdr->sh_addr = 0;
11710 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11711 symstrtab_hdr->sh_entsize = 0;
11712 symstrtab_hdr->sh_link = 0;
11713 symstrtab_hdr->sh_info = 0;
11714 /* sh_offset is set just below. */
11715 symstrtab_hdr->sh_addralign = 1;
11717 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11719 elf_next_file_pos (abfd) = off;
11721 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11722 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11726 /* Adjust the relocs to have the correct symbol indices. */
11727 for (o = abfd->sections; o != NULL; o = o->next)
11729 struct bfd_elf_section_data *esdo = elf_section_data (o);
11731 if ((o->flags & SEC_RELOC) == 0)
11734 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11735 if (esdo->rel.hdr != NULL
11736 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11738 if (esdo->rela.hdr != NULL
11739 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11742 /* Set the reloc_count field to 0 to prevent write_relocs from
11743 trying to swap the relocs out itself. */
11744 o->reloc_count = 0;
11747 if (dynamic && info->combreloc && dynobj != NULL)
11748 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11750 /* If we are linking against a dynamic object, or generating a
11751 shared library, finish up the dynamic linking information. */
11754 bfd_byte *dyncon, *dynconend;
11756 /* Fix up .dynamic entries. */
11757 o = bfd_get_linker_section (dynobj, ".dynamic");
11758 BFD_ASSERT (o != NULL);
11760 dyncon = o->contents;
11761 dynconend = o->contents + o->size;
11762 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11764 Elf_Internal_Dyn dyn;
11768 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11775 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11777 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11779 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11780 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11783 dyn.d_un.d_val = relativecount;
11790 name = info->init_function;
11793 name = info->fini_function;
11796 struct elf_link_hash_entry *h;
11798 h = elf_link_hash_lookup (elf_hash_table (info), name,
11799 FALSE, FALSE, TRUE);
11801 && (h->root.type == bfd_link_hash_defined
11802 || h->root.type == bfd_link_hash_defweak))
11804 dyn.d_un.d_ptr = h->root.u.def.value;
11805 o = h->root.u.def.section;
11806 if (o->output_section != NULL)
11807 dyn.d_un.d_ptr += (o->output_section->vma
11808 + o->output_offset);
11811 /* The symbol is imported from another shared
11812 library and does not apply to this one. */
11813 dyn.d_un.d_ptr = 0;
11820 case DT_PREINIT_ARRAYSZ:
11821 name = ".preinit_array";
11823 case DT_INIT_ARRAYSZ:
11824 name = ".init_array";
11826 case DT_FINI_ARRAYSZ:
11827 name = ".fini_array";
11829 o = bfd_get_section_by_name (abfd, name);
11832 (*_bfd_error_handler)
11833 (_("%B: could not find output section %s"), abfd, name);
11837 (*_bfd_error_handler)
11838 (_("warning: %s section has zero size"), name);
11839 dyn.d_un.d_val = o->size;
11842 case DT_PREINIT_ARRAY:
11843 name = ".preinit_array";
11845 case DT_INIT_ARRAY:
11846 name = ".init_array";
11848 case DT_FINI_ARRAY:
11849 name = ".fini_array";
11856 name = ".gnu.hash";
11865 name = ".gnu.version_d";
11868 name = ".gnu.version_r";
11871 name = ".gnu.version";
11873 o = bfd_get_section_by_name (abfd, name);
11876 (*_bfd_error_handler)
11877 (_("%B: could not find output section %s"), abfd, name);
11880 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11882 (*_bfd_error_handler)
11883 (_("warning: section '%s' is being made into a note"), name);
11884 bfd_set_error (bfd_error_nonrepresentable_section);
11887 dyn.d_un.d_ptr = o->vma;
11894 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11898 dyn.d_un.d_val = 0;
11899 dyn.d_un.d_ptr = 0;
11900 for (i = 1; i < elf_numsections (abfd); i++)
11902 Elf_Internal_Shdr *hdr;
11904 hdr = elf_elfsections (abfd)[i];
11905 if (hdr->sh_type == type
11906 && (hdr->sh_flags & SHF_ALLOC) != 0)
11908 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11909 dyn.d_un.d_val += hdr->sh_size;
11912 if (dyn.d_un.d_ptr == 0
11913 || hdr->sh_addr < dyn.d_un.d_ptr)
11914 dyn.d_un.d_ptr = hdr->sh_addr;
11920 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11924 /* If we have created any dynamic sections, then output them. */
11925 if (dynobj != NULL)
11927 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11930 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11931 if (((info->warn_shared_textrel && bfd_link_pic (info))
11932 || info->error_textrel)
11933 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11935 bfd_byte *dyncon, *dynconend;
11937 dyncon = o->contents;
11938 dynconend = o->contents + o->size;
11939 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11941 Elf_Internal_Dyn dyn;
11943 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11945 if (dyn.d_tag == DT_TEXTREL)
11947 if (info->error_textrel)
11948 info->callbacks->einfo
11949 (_("%P%X: read-only segment has dynamic relocations.\n"));
11951 info->callbacks->einfo
11952 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11958 for (o = dynobj->sections; o != NULL; o = o->next)
11960 if ((o->flags & SEC_HAS_CONTENTS) == 0
11962 || o->output_section == bfd_abs_section_ptr)
11964 if ((o->flags & SEC_LINKER_CREATED) == 0)
11966 /* At this point, we are only interested in sections
11967 created by _bfd_elf_link_create_dynamic_sections. */
11970 if (elf_hash_table (info)->stab_info.stabstr == o)
11972 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11974 if (strcmp (o->name, ".dynstr") != 0)
11976 if (! bfd_set_section_contents (abfd, o->output_section,
11978 (file_ptr) o->output_offset
11979 * bfd_octets_per_byte (abfd),
11985 /* The contents of the .dynstr section are actually in a
11989 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11990 if (bfd_seek (abfd, off, SEEK_SET) != 0
11991 || ! _bfd_elf_strtab_emit (abfd,
11992 elf_hash_table (info)->dynstr))
11998 if (bfd_link_relocatable (info))
12000 bfd_boolean failed = FALSE;
12002 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12007 /* If we have optimized stabs strings, output them. */
12008 if (elf_hash_table (info)->stab_info.stabstr != NULL)
12010 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12014 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12017 elf_final_link_free (abfd, &flinfo);
12019 elf_linker (abfd) = TRUE;
12023 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12024 if (contents == NULL)
12025 return FALSE; /* Bail out and fail. */
12026 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12027 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12034 elf_final_link_free (abfd, &flinfo);
12038 /* Initialize COOKIE for input bfd ABFD. */
12041 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12042 struct bfd_link_info *info, bfd *abfd)
12044 Elf_Internal_Shdr *symtab_hdr;
12045 const struct elf_backend_data *bed;
12047 bed = get_elf_backend_data (abfd);
12048 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12050 cookie->abfd = abfd;
12051 cookie->sym_hashes = elf_sym_hashes (abfd);
12052 cookie->bad_symtab = elf_bad_symtab (abfd);
12053 if (cookie->bad_symtab)
12055 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12056 cookie->extsymoff = 0;
12060 cookie->locsymcount = symtab_hdr->sh_info;
12061 cookie->extsymoff = symtab_hdr->sh_info;
12064 if (bed->s->arch_size == 32)
12065 cookie->r_sym_shift = 8;
12067 cookie->r_sym_shift = 32;
12069 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12070 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12072 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12073 cookie->locsymcount, 0,
12075 if (cookie->locsyms == NULL)
12077 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12080 if (info->keep_memory)
12081 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12086 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12089 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12091 Elf_Internal_Shdr *symtab_hdr;
12093 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12094 if (cookie->locsyms != NULL
12095 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12096 free (cookie->locsyms);
12099 /* Initialize the relocation information in COOKIE for input section SEC
12100 of input bfd ABFD. */
12103 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12104 struct bfd_link_info *info, bfd *abfd,
12107 const struct elf_backend_data *bed;
12109 if (sec->reloc_count == 0)
12111 cookie->rels = NULL;
12112 cookie->relend = NULL;
12116 bed = get_elf_backend_data (abfd);
12118 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12119 info->keep_memory);
12120 if (cookie->rels == NULL)
12122 cookie->rel = cookie->rels;
12123 cookie->relend = (cookie->rels
12124 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12126 cookie->rel = cookie->rels;
12130 /* Free the memory allocated by init_reloc_cookie_rels,
12134 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12137 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12138 free (cookie->rels);
12141 /* Initialize the whole of COOKIE for input section SEC. */
12144 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12145 struct bfd_link_info *info,
12148 if (!init_reloc_cookie (cookie, info, sec->owner))
12150 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12155 fini_reloc_cookie (cookie, sec->owner);
12160 /* Free the memory allocated by init_reloc_cookie_for_section,
12164 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12167 fini_reloc_cookie_rels (cookie, sec);
12168 fini_reloc_cookie (cookie, sec->owner);
12171 /* Garbage collect unused sections. */
12173 /* Default gc_mark_hook. */
12176 _bfd_elf_gc_mark_hook (asection *sec,
12177 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12178 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12179 struct elf_link_hash_entry *h,
12180 Elf_Internal_Sym *sym)
12184 switch (h->root.type)
12186 case bfd_link_hash_defined:
12187 case bfd_link_hash_defweak:
12188 return h->root.u.def.section;
12190 case bfd_link_hash_common:
12191 return h->root.u.c.p->section;
12198 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12203 /* COOKIE->rel describes a relocation against section SEC, which is
12204 a section we've decided to keep. Return the section that contains
12205 the relocation symbol, or NULL if no section contains it. */
12208 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12209 elf_gc_mark_hook_fn gc_mark_hook,
12210 struct elf_reloc_cookie *cookie,
12211 bfd_boolean *start_stop)
12213 unsigned long r_symndx;
12214 struct elf_link_hash_entry *h;
12216 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12217 if (r_symndx == STN_UNDEF)
12220 if (r_symndx >= cookie->locsymcount
12221 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12223 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12226 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12230 while (h->root.type == bfd_link_hash_indirect
12231 || h->root.type == bfd_link_hash_warning)
12232 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12234 /* If this symbol is weak and there is a non-weak definition, we
12235 keep the non-weak definition because many backends put
12236 dynamic reloc info on the non-weak definition for code
12237 handling copy relocs. */
12238 if (h->u.weakdef != NULL)
12239 h->u.weakdef->mark = 1;
12241 if (start_stop != NULL
12242 && (h->root.type == bfd_link_hash_undefined
12243 || h->root.type == bfd_link_hash_undefweak))
12245 /* To work around a glibc bug, mark all XXX input sections
12246 when there is an as yet undefined reference to __start_XXX
12247 or __stop_XXX symbols. The linker will later define such
12248 symbols for orphan input sections that have a name
12249 representable as a C identifier. */
12250 const char *sec_name = NULL;
12251 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12252 sec_name = h->root.root.string + 8;
12253 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12254 sec_name = h->root.root.string + 7;
12256 if (sec_name != NULL && *sec_name != '\0')
12260 for (i = info->input_bfds; i != NULL; i = i->link.next)
12262 asection *s = bfd_get_section_by_name (i, sec_name);
12263 if (s != NULL && !s->gc_mark)
12265 *start_stop = TRUE;
12272 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12275 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12276 &cookie->locsyms[r_symndx]);
12279 /* COOKIE->rel describes a relocation against section SEC, which is
12280 a section we've decided to keep. Mark the section that contains
12281 the relocation symbol. */
12284 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12286 elf_gc_mark_hook_fn gc_mark_hook,
12287 struct elf_reloc_cookie *cookie)
12290 bfd_boolean start_stop = FALSE;
12292 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12293 while (rsec != NULL)
12295 if (!rsec->gc_mark)
12297 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12298 || (rsec->owner->flags & DYNAMIC) != 0)
12300 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12305 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12310 /* The mark phase of garbage collection. For a given section, mark
12311 it and any sections in this section's group, and all the sections
12312 which define symbols to which it refers. */
12315 _bfd_elf_gc_mark (struct bfd_link_info *info,
12317 elf_gc_mark_hook_fn gc_mark_hook)
12320 asection *group_sec, *eh_frame;
12324 /* Mark all the sections in the group. */
12325 group_sec = elf_section_data (sec)->next_in_group;
12326 if (group_sec && !group_sec->gc_mark)
12327 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12330 /* Look through the section relocs. */
12332 eh_frame = elf_eh_frame_section (sec->owner);
12333 if ((sec->flags & SEC_RELOC) != 0
12334 && sec->reloc_count > 0
12335 && sec != eh_frame)
12337 struct elf_reloc_cookie cookie;
12339 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12343 for (; cookie.rel < cookie.relend; cookie.rel++)
12344 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12349 fini_reloc_cookie_for_section (&cookie, sec);
12353 if (ret && eh_frame && elf_fde_list (sec))
12355 struct elf_reloc_cookie cookie;
12357 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12361 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12362 gc_mark_hook, &cookie))
12364 fini_reloc_cookie_for_section (&cookie, eh_frame);
12368 eh_frame = elf_section_eh_frame_entry (sec);
12369 if (ret && eh_frame && !eh_frame->gc_mark)
12370 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12376 /* Scan and mark sections in a special or debug section group. */
12379 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12381 /* Point to first section of section group. */
12383 /* Used to iterate the section group. */
12386 bfd_boolean is_special_grp = TRUE;
12387 bfd_boolean is_debug_grp = TRUE;
12389 /* First scan to see if group contains any section other than debug
12390 and special section. */
12391 ssec = msec = elf_next_in_group (grp);
12394 if ((msec->flags & SEC_DEBUGGING) == 0)
12395 is_debug_grp = FALSE;
12397 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12398 is_special_grp = FALSE;
12400 msec = elf_next_in_group (msec);
12402 while (msec != ssec);
12404 /* If this is a pure debug section group or pure special section group,
12405 keep all sections in this group. */
12406 if (is_debug_grp || is_special_grp)
12411 msec = elf_next_in_group (msec);
12413 while (msec != ssec);
12417 /* Keep debug and special sections. */
12420 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12421 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12425 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12428 bfd_boolean some_kept;
12429 bfd_boolean debug_frag_seen;
12431 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12434 /* Ensure all linker created sections are kept,
12435 see if any other section is already marked,
12436 and note if we have any fragmented debug sections. */
12437 debug_frag_seen = some_kept = FALSE;
12438 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12440 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12442 else if (isec->gc_mark)
12445 if (debug_frag_seen == FALSE
12446 && (isec->flags & SEC_DEBUGGING)
12447 && CONST_STRNEQ (isec->name, ".debug_line."))
12448 debug_frag_seen = TRUE;
12451 /* If no section in this file will be kept, then we can
12452 toss out the debug and special sections. */
12456 /* Keep debug and special sections like .comment when they are
12457 not part of a group. Also keep section groups that contain
12458 just debug sections or special sections. */
12459 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12461 if ((isec->flags & SEC_GROUP) != 0)
12462 _bfd_elf_gc_mark_debug_special_section_group (isec);
12463 else if (((isec->flags & SEC_DEBUGGING) != 0
12464 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12465 && elf_next_in_group (isec) == NULL)
12469 if (! debug_frag_seen)
12472 /* Look for CODE sections which are going to be discarded,
12473 and find and discard any fragmented debug sections which
12474 are associated with that code section. */
12475 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12476 if ((isec->flags & SEC_CODE) != 0
12477 && isec->gc_mark == 0)
12482 ilen = strlen (isec->name);
12484 /* Association is determined by the name of the debug section
12485 containing the name of the code section as a suffix. For
12486 example .debug_line.text.foo is a debug section associated
12488 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12492 if (dsec->gc_mark == 0
12493 || (dsec->flags & SEC_DEBUGGING) == 0)
12496 dlen = strlen (dsec->name);
12499 && strncmp (dsec->name + (dlen - ilen),
12500 isec->name, ilen) == 0)
12510 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12512 struct elf_gc_sweep_symbol_info
12514 struct bfd_link_info *info;
12515 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12520 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12523 && (((h->root.type == bfd_link_hash_defined
12524 || h->root.type == bfd_link_hash_defweak)
12525 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12526 && h->root.u.def.section->gc_mark))
12527 || h->root.type == bfd_link_hash_undefined
12528 || h->root.type == bfd_link_hash_undefweak))
12530 struct elf_gc_sweep_symbol_info *inf;
12532 inf = (struct elf_gc_sweep_symbol_info *) data;
12533 (*inf->hide_symbol) (inf->info, h, TRUE);
12534 h->def_regular = 0;
12535 h->ref_regular = 0;
12536 h->ref_regular_nonweak = 0;
12542 /* The sweep phase of garbage collection. Remove all garbage sections. */
12544 typedef bfd_boolean (*gc_sweep_hook_fn)
12545 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12548 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12551 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12552 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12553 unsigned long section_sym_count;
12554 struct elf_gc_sweep_symbol_info sweep_info;
12556 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12560 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12561 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12564 for (o = sub->sections; o != NULL; o = o->next)
12566 /* When any section in a section group is kept, we keep all
12567 sections in the section group. If the first member of
12568 the section group is excluded, we will also exclude the
12570 if (o->flags & SEC_GROUP)
12572 asection *first = elf_next_in_group (o);
12573 o->gc_mark = first->gc_mark;
12579 /* Skip sweeping sections already excluded. */
12580 if (o->flags & SEC_EXCLUDE)
12583 /* Since this is early in the link process, it is simple
12584 to remove a section from the output. */
12585 o->flags |= SEC_EXCLUDE;
12587 if (info->print_gc_sections && o->size != 0)
12588 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12590 /* But we also have to update some of the relocation
12591 info we collected before. */
12593 && (o->flags & SEC_RELOC) != 0
12594 && o->reloc_count != 0
12595 && !((info->strip == strip_all || info->strip == strip_debugger)
12596 && (o->flags & SEC_DEBUGGING) != 0)
12597 && !bfd_is_abs_section (o->output_section))
12599 Elf_Internal_Rela *internal_relocs;
12603 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12604 info->keep_memory);
12605 if (internal_relocs == NULL)
12608 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12610 if (elf_section_data (o)->relocs != internal_relocs)
12611 free (internal_relocs);
12619 /* Remove the symbols that were in the swept sections from the dynamic
12620 symbol table. GCFIXME: Anyone know how to get them out of the
12621 static symbol table as well? */
12622 sweep_info.info = info;
12623 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12624 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12627 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
12631 /* Propagate collected vtable information. This is called through
12632 elf_link_hash_traverse. */
12635 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12637 /* Those that are not vtables. */
12638 if (h->vtable == NULL || h->vtable->parent == NULL)
12641 /* Those vtables that do not have parents, we cannot merge. */
12642 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12645 /* If we've already been done, exit. */
12646 if (h->vtable->used && h->vtable->used[-1])
12649 /* Make sure the parent's table is up to date. */
12650 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12652 if (h->vtable->used == NULL)
12654 /* None of this table's entries were referenced. Re-use the
12656 h->vtable->used = h->vtable->parent->vtable->used;
12657 h->vtable->size = h->vtable->parent->vtable->size;
12662 bfd_boolean *cu, *pu;
12664 /* Or the parent's entries into ours. */
12665 cu = h->vtable->used;
12667 pu = h->vtable->parent->vtable->used;
12670 const struct elf_backend_data *bed;
12671 unsigned int log_file_align;
12673 bed = get_elf_backend_data (h->root.u.def.section->owner);
12674 log_file_align = bed->s->log_file_align;
12675 n = h->vtable->parent->vtable->size >> log_file_align;
12690 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12693 bfd_vma hstart, hend;
12694 Elf_Internal_Rela *relstart, *relend, *rel;
12695 const struct elf_backend_data *bed;
12696 unsigned int log_file_align;
12698 /* Take care of both those symbols that do not describe vtables as
12699 well as those that are not loaded. */
12700 if (h->vtable == NULL || h->vtable->parent == NULL)
12703 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12704 || h->root.type == bfd_link_hash_defweak);
12706 sec = h->root.u.def.section;
12707 hstart = h->root.u.def.value;
12708 hend = hstart + h->size;
12710 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12712 return *(bfd_boolean *) okp = FALSE;
12713 bed = get_elf_backend_data (sec->owner);
12714 log_file_align = bed->s->log_file_align;
12716 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12718 for (rel = relstart; rel < relend; ++rel)
12719 if (rel->r_offset >= hstart && rel->r_offset < hend)
12721 /* If the entry is in use, do nothing. */
12722 if (h->vtable->used
12723 && (rel->r_offset - hstart) < h->vtable->size)
12725 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12726 if (h->vtable->used[entry])
12729 /* Otherwise, kill it. */
12730 rel->r_offset = rel->r_info = rel->r_addend = 0;
12736 /* Mark sections containing dynamically referenced symbols. When
12737 building shared libraries, we must assume that any visible symbol is
12741 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12743 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12744 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12746 if ((h->root.type == bfd_link_hash_defined
12747 || h->root.type == bfd_link_hash_defweak)
12749 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12750 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12751 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12752 && (!bfd_link_executable (info)
12753 || info->export_dynamic
12756 && (*d->match) (&d->head, NULL, h->root.root.string)))
12757 && (h->versioned >= versioned
12758 || !bfd_hide_sym_by_version (info->version_info,
12759 h->root.root.string)))))
12760 h->root.u.def.section->flags |= SEC_KEEP;
12765 /* Keep all sections containing symbols undefined on the command-line,
12766 and the section containing the entry symbol. */
12769 _bfd_elf_gc_keep (struct bfd_link_info *info)
12771 struct bfd_sym_chain *sym;
12773 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12775 struct elf_link_hash_entry *h;
12777 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12778 FALSE, FALSE, FALSE);
12781 && (h->root.type == bfd_link_hash_defined
12782 || h->root.type == bfd_link_hash_defweak)
12783 && !bfd_is_abs_section (h->root.u.def.section))
12784 h->root.u.def.section->flags |= SEC_KEEP;
12789 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12790 struct bfd_link_info *info)
12792 bfd *ibfd = info->input_bfds;
12794 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12797 struct elf_reloc_cookie cookie;
12799 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12802 if (!init_reloc_cookie (&cookie, info, ibfd))
12805 for (sec = ibfd->sections; sec; sec = sec->next)
12807 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12808 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12810 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12811 fini_reloc_cookie_rels (&cookie, sec);
12818 /* Do mark and sweep of unused sections. */
12821 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12823 bfd_boolean ok = TRUE;
12825 elf_gc_mark_hook_fn gc_mark_hook;
12826 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12827 struct elf_link_hash_table *htab;
12829 if (!bed->can_gc_sections
12830 || !is_elf_hash_table (info->hash))
12832 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12836 bed->gc_keep (info);
12837 htab = elf_hash_table (info);
12839 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12840 at the .eh_frame section if we can mark the FDEs individually. */
12841 for (sub = info->input_bfds;
12842 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12843 sub = sub->link.next)
12846 struct elf_reloc_cookie cookie;
12848 sec = bfd_get_section_by_name (sub, ".eh_frame");
12849 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12851 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12852 if (elf_section_data (sec)->sec_info
12853 && (sec->flags & SEC_LINKER_CREATED) == 0)
12854 elf_eh_frame_section (sub) = sec;
12855 fini_reloc_cookie_for_section (&cookie, sec);
12856 sec = bfd_get_next_section_by_name (NULL, sec);
12860 /* Apply transitive closure to the vtable entry usage info. */
12861 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12865 /* Kill the vtable relocations that were not used. */
12866 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12870 /* Mark dynamically referenced symbols. */
12871 if (htab->dynamic_sections_created)
12872 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12874 /* Grovel through relocs to find out who stays ... */
12875 gc_mark_hook = bed->gc_mark_hook;
12876 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12880 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12881 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12884 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12885 Also treat note sections as a root, if the section is not part
12887 for (o = sub->sections; o != NULL; o = o->next)
12889 && (o->flags & SEC_EXCLUDE) == 0
12890 && ((o->flags & SEC_KEEP) != 0
12891 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12892 && elf_next_in_group (o) == NULL )))
12894 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12899 /* Allow the backend to mark additional target specific sections. */
12900 bed->gc_mark_extra_sections (info, gc_mark_hook);
12902 /* ... and mark SEC_EXCLUDE for those that go. */
12903 return elf_gc_sweep (abfd, info);
12906 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12909 bfd_elf_gc_record_vtinherit (bfd *abfd,
12911 struct elf_link_hash_entry *h,
12914 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12915 struct elf_link_hash_entry **search, *child;
12916 bfd_size_type extsymcount;
12917 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12919 /* The sh_info field of the symtab header tells us where the
12920 external symbols start. We don't care about the local symbols at
12922 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12923 if (!elf_bad_symtab (abfd))
12924 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12926 sym_hashes = elf_sym_hashes (abfd);
12927 sym_hashes_end = sym_hashes + extsymcount;
12929 /* Hunt down the child symbol, which is in this section at the same
12930 offset as the relocation. */
12931 for (search = sym_hashes; search != sym_hashes_end; ++search)
12933 if ((child = *search) != NULL
12934 && (child->root.type == bfd_link_hash_defined
12935 || child->root.type == bfd_link_hash_defweak)
12936 && child->root.u.def.section == sec
12937 && child->root.u.def.value == offset)
12941 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12942 abfd, sec, (unsigned long) offset);
12943 bfd_set_error (bfd_error_invalid_operation);
12947 if (!child->vtable)
12949 child->vtable = ((struct elf_link_virtual_table_entry *)
12950 bfd_zalloc (abfd, sizeof (*child->vtable)));
12951 if (!child->vtable)
12956 /* This *should* only be the absolute section. It could potentially
12957 be that someone has defined a non-global vtable though, which
12958 would be bad. It isn't worth paging in the local symbols to be
12959 sure though; that case should simply be handled by the assembler. */
12961 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12964 child->vtable->parent = h;
12969 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12972 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12973 asection *sec ATTRIBUTE_UNUSED,
12974 struct elf_link_hash_entry *h,
12977 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12978 unsigned int log_file_align = bed->s->log_file_align;
12982 h->vtable = ((struct elf_link_virtual_table_entry *)
12983 bfd_zalloc (abfd, sizeof (*h->vtable)));
12988 if (addend >= h->vtable->size)
12990 size_t size, bytes, file_align;
12991 bfd_boolean *ptr = h->vtable->used;
12993 /* While the symbol is undefined, we have to be prepared to handle
12995 file_align = 1 << log_file_align;
12996 if (h->root.type == bfd_link_hash_undefined)
12997 size = addend + file_align;
13001 if (addend >= size)
13003 /* Oops! We've got a reference past the defined end of
13004 the table. This is probably a bug -- shall we warn? */
13005 size = addend + file_align;
13008 size = (size + file_align - 1) & -file_align;
13010 /* Allocate one extra entry for use as a "done" flag for the
13011 consolidation pass. */
13012 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13016 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13022 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13023 * sizeof (bfd_boolean));
13024 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13028 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13033 /* And arrange for that done flag to be at index -1. */
13034 h->vtable->used = ptr + 1;
13035 h->vtable->size = size;
13038 h->vtable->used[addend >> log_file_align] = TRUE;
13043 /* Map an ELF section header flag to its corresponding string. */
13047 flagword flag_value;
13048 } elf_flags_to_name_table;
13050 static elf_flags_to_name_table elf_flags_to_names [] =
13052 { "SHF_WRITE", SHF_WRITE },
13053 { "SHF_ALLOC", SHF_ALLOC },
13054 { "SHF_EXECINSTR", SHF_EXECINSTR },
13055 { "SHF_MERGE", SHF_MERGE },
13056 { "SHF_STRINGS", SHF_STRINGS },
13057 { "SHF_INFO_LINK", SHF_INFO_LINK},
13058 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13059 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13060 { "SHF_GROUP", SHF_GROUP },
13061 { "SHF_TLS", SHF_TLS },
13062 { "SHF_MASKOS", SHF_MASKOS },
13063 { "SHF_EXCLUDE", SHF_EXCLUDE },
13066 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13068 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13069 struct flag_info *flaginfo,
13072 const bfd_vma sh_flags = elf_section_flags (section);
13074 if (!flaginfo->flags_initialized)
13076 bfd *obfd = info->output_bfd;
13077 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13078 struct flag_info_list *tf = flaginfo->flag_list;
13080 int without_hex = 0;
13082 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13085 flagword (*lookup) (char *);
13087 lookup = bed->elf_backend_lookup_section_flags_hook;
13088 if (lookup != NULL)
13090 flagword hexval = (*lookup) ((char *) tf->name);
13094 if (tf->with == with_flags)
13095 with_hex |= hexval;
13096 else if (tf->with == without_flags)
13097 without_hex |= hexval;
13102 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13104 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13106 if (tf->with == with_flags)
13107 with_hex |= elf_flags_to_names[i].flag_value;
13108 else if (tf->with == without_flags)
13109 without_hex |= elf_flags_to_names[i].flag_value;
13116 info->callbacks->einfo
13117 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13121 flaginfo->flags_initialized = TRUE;
13122 flaginfo->only_with_flags |= with_hex;
13123 flaginfo->not_with_flags |= without_hex;
13126 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13129 if ((flaginfo->not_with_flags & sh_flags) != 0)
13135 struct alloc_got_off_arg {
13137 struct bfd_link_info *info;
13140 /* We need a special top-level link routine to convert got reference counts
13141 to real got offsets. */
13144 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13146 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13147 bfd *obfd = gofarg->info->output_bfd;
13148 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13150 if (h->got.refcount > 0)
13152 h->got.offset = gofarg->gotoff;
13153 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13156 h->got.offset = (bfd_vma) -1;
13161 /* And an accompanying bit to work out final got entry offsets once
13162 we're done. Should be called from final_link. */
13165 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13166 struct bfd_link_info *info)
13169 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13171 struct alloc_got_off_arg gofarg;
13173 BFD_ASSERT (abfd == info->output_bfd);
13175 if (! is_elf_hash_table (info->hash))
13178 /* The GOT offset is relative to the .got section, but the GOT header is
13179 put into the .got.plt section, if the backend uses it. */
13180 if (bed->want_got_plt)
13183 gotoff = bed->got_header_size;
13185 /* Do the local .got entries first. */
13186 for (i = info->input_bfds; i; i = i->link.next)
13188 bfd_signed_vma *local_got;
13189 bfd_size_type j, locsymcount;
13190 Elf_Internal_Shdr *symtab_hdr;
13192 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13195 local_got = elf_local_got_refcounts (i);
13199 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13200 if (elf_bad_symtab (i))
13201 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13203 locsymcount = symtab_hdr->sh_info;
13205 for (j = 0; j < locsymcount; ++j)
13207 if (local_got[j] > 0)
13209 local_got[j] = gotoff;
13210 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13213 local_got[j] = (bfd_vma) -1;
13217 /* Then the global .got entries. .plt refcounts are handled by
13218 adjust_dynamic_symbol */
13219 gofarg.gotoff = gotoff;
13220 gofarg.info = info;
13221 elf_link_hash_traverse (elf_hash_table (info),
13222 elf_gc_allocate_got_offsets,
13227 /* Many folk need no more in the way of final link than this, once
13228 got entry reference counting is enabled. */
13231 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13233 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13236 /* Invoke the regular ELF backend linker to do all the work. */
13237 return bfd_elf_final_link (abfd, info);
13241 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13243 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13245 if (rcookie->bad_symtab)
13246 rcookie->rel = rcookie->rels;
13248 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13250 unsigned long r_symndx;
13252 if (! rcookie->bad_symtab)
13253 if (rcookie->rel->r_offset > offset)
13255 if (rcookie->rel->r_offset != offset)
13258 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13259 if (r_symndx == STN_UNDEF)
13262 if (r_symndx >= rcookie->locsymcount
13263 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13265 struct elf_link_hash_entry *h;
13267 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13269 while (h->root.type == bfd_link_hash_indirect
13270 || h->root.type == bfd_link_hash_warning)
13271 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13273 if ((h->root.type == bfd_link_hash_defined
13274 || h->root.type == bfd_link_hash_defweak)
13275 && (h->root.u.def.section->owner != rcookie->abfd
13276 || h->root.u.def.section->kept_section != NULL
13277 || discarded_section (h->root.u.def.section)))
13282 /* It's not a relocation against a global symbol,
13283 but it could be a relocation against a local
13284 symbol for a discarded section. */
13286 Elf_Internal_Sym *isym;
13288 /* Need to: get the symbol; get the section. */
13289 isym = &rcookie->locsyms[r_symndx];
13290 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13292 && (isec->kept_section != NULL
13293 || discarded_section (isec)))
13301 /* Discard unneeded references to discarded sections.
13302 Returns -1 on error, 1 if any section's size was changed, 0 if
13303 nothing changed. This function assumes that the relocations are in
13304 sorted order, which is true for all known assemblers. */
13307 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13309 struct elf_reloc_cookie cookie;
13314 if (info->traditional_format
13315 || !is_elf_hash_table (info->hash))
13318 o = bfd_get_section_by_name (output_bfd, ".stab");
13323 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13326 || i->reloc_count == 0
13327 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13331 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13334 if (!init_reloc_cookie_for_section (&cookie, info, i))
13337 if (_bfd_discard_section_stabs (abfd, i,
13338 elf_section_data (i)->sec_info,
13339 bfd_elf_reloc_symbol_deleted_p,
13343 fini_reloc_cookie_for_section (&cookie, i);
13348 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13349 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13354 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13360 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13363 if (!init_reloc_cookie_for_section (&cookie, info, i))
13366 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13367 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13368 bfd_elf_reloc_symbol_deleted_p,
13372 fini_reloc_cookie_for_section (&cookie, i);
13376 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13378 const struct elf_backend_data *bed;
13380 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13383 bed = get_elf_backend_data (abfd);
13385 if (bed->elf_backend_discard_info != NULL)
13387 if (!init_reloc_cookie (&cookie, info, abfd))
13390 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13393 fini_reloc_cookie (&cookie, abfd);
13397 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13398 _bfd_elf_end_eh_frame_parsing (info);
13400 if (info->eh_frame_hdr_type
13401 && !bfd_link_relocatable (info)
13402 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13409 _bfd_elf_section_already_linked (bfd *abfd,
13411 struct bfd_link_info *info)
13414 const char *name, *key;
13415 struct bfd_section_already_linked *l;
13416 struct bfd_section_already_linked_hash_entry *already_linked_list;
13418 if (sec->output_section == bfd_abs_section_ptr)
13421 flags = sec->flags;
13423 /* Return if it isn't a linkonce section. A comdat group section
13424 also has SEC_LINK_ONCE set. */
13425 if ((flags & SEC_LINK_ONCE) == 0)
13428 /* Don't put group member sections on our list of already linked
13429 sections. They are handled as a group via their group section. */
13430 if (elf_sec_group (sec) != NULL)
13433 /* For a SHT_GROUP section, use the group signature as the key. */
13435 if ((flags & SEC_GROUP) != 0
13436 && elf_next_in_group (sec) != NULL
13437 && elf_group_name (elf_next_in_group (sec)) != NULL)
13438 key = elf_group_name (elf_next_in_group (sec));
13441 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13442 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13443 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13446 /* Must be a user linkonce section that doesn't follow gcc's
13447 naming convention. In this case we won't be matching
13448 single member groups. */
13452 already_linked_list = bfd_section_already_linked_table_lookup (key);
13454 for (l = already_linked_list->entry; l != NULL; l = l->next)
13456 /* We may have 2 different types of sections on the list: group
13457 sections with a signature of <key> (<key> is some string),
13458 and linkonce sections named .gnu.linkonce.<type>.<key>.
13459 Match like sections. LTO plugin sections are an exception.
13460 They are always named .gnu.linkonce.t.<key> and match either
13461 type of section. */
13462 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13463 && ((flags & SEC_GROUP) != 0
13464 || strcmp (name, l->sec->name) == 0))
13465 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13467 /* The section has already been linked. See if we should
13468 issue a warning. */
13469 if (!_bfd_handle_already_linked (sec, l, info))
13472 if (flags & SEC_GROUP)
13474 asection *first = elf_next_in_group (sec);
13475 asection *s = first;
13479 s->output_section = bfd_abs_section_ptr;
13480 /* Record which group discards it. */
13481 s->kept_section = l->sec;
13482 s = elf_next_in_group (s);
13483 /* These lists are circular. */
13493 /* A single member comdat group section may be discarded by a
13494 linkonce section and vice versa. */
13495 if ((flags & SEC_GROUP) != 0)
13497 asection *first = elf_next_in_group (sec);
13499 if (first != NULL && elf_next_in_group (first) == first)
13500 /* Check this single member group against linkonce sections. */
13501 for (l = already_linked_list->entry; l != NULL; l = l->next)
13502 if ((l->sec->flags & SEC_GROUP) == 0
13503 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13505 first->output_section = bfd_abs_section_ptr;
13506 first->kept_section = l->sec;
13507 sec->output_section = bfd_abs_section_ptr;
13512 /* Check this linkonce section against single member groups. */
13513 for (l = already_linked_list->entry; l != NULL; l = l->next)
13514 if (l->sec->flags & SEC_GROUP)
13516 asection *first = elf_next_in_group (l->sec);
13519 && elf_next_in_group (first) == first
13520 && bfd_elf_match_symbols_in_sections (first, sec, info))
13522 sec->output_section = bfd_abs_section_ptr;
13523 sec->kept_section = first;
13528 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13529 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13530 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13531 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13532 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13533 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13534 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13535 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13536 The reverse order cannot happen as there is never a bfd with only the
13537 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13538 matter as here were are looking only for cross-bfd sections. */
13540 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13541 for (l = already_linked_list->entry; l != NULL; l = l->next)
13542 if ((l->sec->flags & SEC_GROUP) == 0
13543 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13545 if (abfd != l->sec->owner)
13546 sec->output_section = bfd_abs_section_ptr;
13550 /* This is the first section with this name. Record it. */
13551 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13552 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13553 return sec->output_section == bfd_abs_section_ptr;
13557 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13559 return sym->st_shndx == SHN_COMMON;
13563 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13569 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13571 return bfd_com_section_ptr;
13575 _bfd_elf_default_got_elt_size (bfd *abfd,
13576 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13577 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13578 bfd *ibfd ATTRIBUTE_UNUSED,
13579 unsigned long symndx ATTRIBUTE_UNUSED)
13581 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13582 return bed->s->arch_size / 8;
13585 /* Routines to support the creation of dynamic relocs. */
13587 /* Returns the name of the dynamic reloc section associated with SEC. */
13589 static const char *
13590 get_dynamic_reloc_section_name (bfd * abfd,
13592 bfd_boolean is_rela)
13595 const char *old_name = bfd_get_section_name (NULL, sec);
13596 const char *prefix = is_rela ? ".rela" : ".rel";
13598 if (old_name == NULL)
13601 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13602 sprintf (name, "%s%s", prefix, old_name);
13607 /* Returns the dynamic reloc section associated with SEC.
13608 If necessary compute the name of the dynamic reloc section based
13609 on SEC's name (looked up in ABFD's string table) and the setting
13613 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13615 bfd_boolean is_rela)
13617 asection * reloc_sec = elf_section_data (sec)->sreloc;
13619 if (reloc_sec == NULL)
13621 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13625 reloc_sec = bfd_get_linker_section (abfd, name);
13627 if (reloc_sec != NULL)
13628 elf_section_data (sec)->sreloc = reloc_sec;
13635 /* Returns the dynamic reloc section associated with SEC. If the
13636 section does not exist it is created and attached to the DYNOBJ
13637 bfd and stored in the SRELOC field of SEC's elf_section_data
13640 ALIGNMENT is the alignment for the newly created section and
13641 IS_RELA defines whether the name should be .rela.<SEC's name>
13642 or .rel.<SEC's name>. The section name is looked up in the
13643 string table associated with ABFD. */
13646 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13648 unsigned int alignment,
13650 bfd_boolean is_rela)
13652 asection * reloc_sec = elf_section_data (sec)->sreloc;
13654 if (reloc_sec == NULL)
13656 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13661 reloc_sec = bfd_get_linker_section (dynobj, name);
13663 if (reloc_sec == NULL)
13665 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13666 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13667 if ((sec->flags & SEC_ALLOC) != 0)
13668 flags |= SEC_ALLOC | SEC_LOAD;
13670 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13671 if (reloc_sec != NULL)
13673 /* _bfd_elf_get_sec_type_attr chooses a section type by
13674 name. Override as it may be wrong, eg. for a user
13675 section named "auto" we'll get ".relauto" which is
13676 seen to be a .rela section. */
13677 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13678 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13683 elf_section_data (sec)->sreloc = reloc_sec;
13689 /* Copy the ELF symbol type and other attributes for a linker script
13690 assignment from HSRC to HDEST. Generally this should be treated as
13691 if we found a strong non-dynamic definition for HDEST (except that
13692 ld ignores multiple definition errors). */
13694 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13695 struct bfd_link_hash_entry *hdest,
13696 struct bfd_link_hash_entry *hsrc)
13698 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13699 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13700 Elf_Internal_Sym isym;
13702 ehdest->type = ehsrc->type;
13703 ehdest->target_internal = ehsrc->target_internal;
13705 isym.st_other = ehsrc->other;
13706 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13709 /* Append a RELA relocation REL to section S in BFD. */
13712 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13714 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13715 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13716 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13717 bed->s->swap_reloca_out (abfd, rel, loc);
13720 /* Append a REL relocation REL to section S in BFD. */
13723 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13725 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13726 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13727 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13728 bed->s->swap_reloc_out (abfd, rel, loc);