1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2017 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"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
39 struct elf_info_failed
41 struct bfd_link_info *info;
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
48 struct elf_find_verdep_info
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
54 /* Whether we had a failure. */
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
69 struct elf_link_hash_entry *h;
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
90 Elf_Internal_Sym *isym;
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
96 && discard ? discarded_section (isec) : 1)
102 /* Define a symbol in a dynamic linkage section. */
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
127 bed = get_elf_backend_data (abfd);
128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 sec, 0, NULL, FALSE, bed->collect,
132 h = (struct elf_link_hash_entry *) bh;
133 BFD_ASSERT (h != NULL);
136 h->root.linker_def = 1;
137 h->type = STT_OBJECT;
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
150 struct elf_link_hash_entry *h;
151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 struct elf_link_hash_table *htab = elf_hash_table (info);
154 /* This function may be called more than once. */
155 if (htab->sgot != NULL)
158 flags = bed->dynamic_sec_flags;
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
176 if (bed->want_got_plt)
178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
189 if (bed->want_got_sym)
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info)->hgot = h;
205 /* Create a strtab to hold the dynamic symbol names. */
207 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
209 struct elf_link_hash_table *hash_table;
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
221 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
223 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
229 hash_table->dynobj = abfd;
232 if (hash_table->dynstr == NULL)
234 hash_table->dynstr = _bfd_elf_strtab_init ();
235 if (hash_table->dynstr == NULL)
241 /* Create some sections which will be filled in with dynamic linking
242 information. ABFD is an input file which requires dynamic sections
243 to be created. The dynamic sections take up virtual memory space
244 when the final executable is run, so we need to create them before
245 addresses are assigned to the output sections. We work out the
246 actual contents and size of these sections later. */
249 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
253 const struct elf_backend_data *bed;
254 struct elf_link_hash_entry *h;
256 if (! is_elf_hash_table (info->hash))
259 if (elf_hash_table (info)->dynamic_sections_created)
262 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
265 abfd = elf_hash_table (info)->dynobj;
266 bed = get_elf_backend_data (abfd);
268 flags = bed->dynamic_sec_flags;
270 /* A dynamically linked executable has a .interp section, but a
271 shared library does not. */
272 if (bfd_link_executable (info) && !info->nointerp)
274 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
275 flags | SEC_READONLY);
280 /* Create sections to hold version informations. These are removed
281 if they are not needed. */
282 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
283 flags | SEC_READONLY);
285 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
288 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
289 flags | SEC_READONLY);
291 || ! bfd_set_section_alignment (abfd, s, 1))
294 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
295 flags | SEC_READONLY);
297 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
300 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
301 flags | SEC_READONLY);
303 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
305 elf_hash_table (info)->dynsym = s;
307 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
308 flags | SEC_READONLY);
312 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
314 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
317 /* The special symbol _DYNAMIC is always set to the start of the
318 .dynamic section. We could set _DYNAMIC in a linker script, but we
319 only want to define it if we are, in fact, creating a .dynamic
320 section. We don't want to define it if there is no .dynamic
321 section, since on some ELF platforms the start up code examines it
322 to decide how to initialize the process. */
323 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
324 elf_hash_table (info)->hdynamic = h;
330 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
331 flags | SEC_READONLY);
333 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
335 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
338 if (info->emit_gnu_hash)
340 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
341 flags | SEC_READONLY);
343 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
345 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
346 4 32-bit words followed by variable count of 64-bit words, then
347 variable count of 32-bit words. */
348 if (bed->s->arch_size == 64)
349 elf_section_data (s)->this_hdr.sh_entsize = 0;
351 elf_section_data (s)->this_hdr.sh_entsize = 4;
354 /* Let the backend create the rest of the sections. This lets the
355 backend set the right flags. The backend will normally create
356 the .got and .plt sections. */
357 if (bed->elf_backend_create_dynamic_sections == NULL
358 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
361 elf_hash_table (info)->dynamic_sections_created = TRUE;
366 /* Create dynamic sections when linking against a dynamic object. */
369 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
371 flagword flags, pltflags;
372 struct elf_link_hash_entry *h;
374 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
375 struct elf_link_hash_table *htab = elf_hash_table (info);
377 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
378 .rel[a].bss sections. */
379 flags = bed->dynamic_sec_flags;
382 if (bed->plt_not_loaded)
383 /* We do not clear SEC_ALLOC here because we still want the OS to
384 allocate space for the section; it's just that there's nothing
385 to read in from the object file. */
386 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
388 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
389 if (bed->plt_readonly)
390 pltflags |= SEC_READONLY;
392 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
394 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
398 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
400 if (bed->want_plt_sym)
402 h = _bfd_elf_define_linkage_sym (abfd, info, s,
403 "_PROCEDURE_LINKAGE_TABLE_");
404 elf_hash_table (info)->hplt = h;
409 s = bfd_make_section_anyway_with_flags (abfd,
410 (bed->rela_plts_and_copies_p
411 ? ".rela.plt" : ".rel.plt"),
412 flags | SEC_READONLY);
414 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
418 if (! _bfd_elf_create_got_section (abfd, info))
421 if (bed->want_dynbss)
423 /* The .dynbss section is a place to put symbols which are defined
424 by dynamic objects, are referenced by regular objects, and are
425 not functions. We must allocate space for them in the process
426 image and use a R_*_COPY reloc to tell the dynamic linker to
427 initialize them at run time. The linker script puts the .dynbss
428 section into the .bss section of the final image. */
429 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
430 SEC_ALLOC | SEC_LINKER_CREATED);
435 if (bed->want_dynrelro)
437 /* Similarly, but for symbols that were originally in read-only
438 sections. This section doesn't really need to have contents,
439 but make it like other .data.rel.ro sections. */
440 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
447 /* The .rel[a].bss section holds copy relocs. This section is not
448 normally needed. We need to create it here, though, so that the
449 linker will map it to an output section. We can't just create it
450 only if we need it, because we will not know whether we need it
451 until we have seen all the input files, and the first time the
452 main linker code calls BFD after examining all the input files
453 (size_dynamic_sections) the input sections have already been
454 mapped to the output sections. If the section turns out not to
455 be needed, we can discard it later. We will never need this
456 section when generating a shared object, since they do not use
458 if (bfd_link_executable (info))
460 s = bfd_make_section_anyway_with_flags (abfd,
461 (bed->rela_plts_and_copies_p
462 ? ".rela.bss" : ".rel.bss"),
463 flags | SEC_READONLY);
465 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
469 if (bed->want_dynrelro)
471 s = (bfd_make_section_anyway_with_flags
472 (abfd, (bed->rela_plts_and_copies_p
473 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
474 flags | SEC_READONLY));
476 || ! bfd_set_section_alignment (abfd, s,
477 bed->s->log_file_align))
479 htab->sreldynrelro = s;
487 /* Record a new dynamic symbol. We record the dynamic symbols as we
488 read the input files, since we need to have a list of all of them
489 before we can determine the final sizes of the output sections.
490 Note that we may actually call this function even though we are not
491 going to output any dynamic symbols; in some cases we know that a
492 symbol should be in the dynamic symbol table, but only if there is
496 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
497 struct elf_link_hash_entry *h)
499 if (h->dynindx == -1)
501 struct elf_strtab_hash *dynstr;
506 /* XXX: The ABI draft says the linker must turn hidden and
507 internal symbols into STB_LOCAL symbols when producing the
508 DSO. However, if ld.so honors st_other in the dynamic table,
509 this would not be necessary. */
510 switch (ELF_ST_VISIBILITY (h->other))
514 if (h->root.type != bfd_link_hash_undefined
515 && h->root.type != bfd_link_hash_undefweak)
518 if (!elf_hash_table (info)->is_relocatable_executable)
526 h->dynindx = elf_hash_table (info)->dynsymcount;
527 ++elf_hash_table (info)->dynsymcount;
529 dynstr = elf_hash_table (info)->dynstr;
532 /* Create a strtab to hold the dynamic symbol names. */
533 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
538 /* We don't put any version information in the dynamic string
540 name = h->root.root.string;
541 p = strchr (name, ELF_VER_CHR);
543 /* We know that the p points into writable memory. In fact,
544 there are only a few symbols that have read-only names, being
545 those like _GLOBAL_OFFSET_TABLE_ that are created specially
546 by the backends. Most symbols will have names pointing into
547 an ELF string table read from a file, or to objalloc memory. */
550 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555 if (indx == (size_t) -1)
557 h->dynstr_index = indx;
563 /* Mark a symbol dynamic. */
566 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
567 struct elf_link_hash_entry *h,
568 Elf_Internal_Sym *sym)
570 struct bfd_elf_dynamic_list *d = info->dynamic_list;
572 /* It may be called more than once on the same H. */
573 if(h->dynamic || bfd_link_relocatable (info))
576 if ((info->dynamic_data
577 && (h->type == STT_OBJECT
578 || h->type == STT_COMMON
580 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
581 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
584 && (*d->match) (&d->head, NULL, h->root.root.string)))
588 /* Record an assignment to a symbol made by a linker script. We need
589 this in case some dynamic object refers to this symbol. */
592 bfd_elf_record_link_assignment (bfd *output_bfd,
593 struct bfd_link_info *info,
598 struct elf_link_hash_entry *h, *hv;
599 struct elf_link_hash_table *htab;
600 const struct elf_backend_data *bed;
602 if (!is_elf_hash_table (info->hash))
605 htab = elf_hash_table (info);
606 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
610 if (h->root.type == bfd_link_hash_warning)
611 h = (struct elf_link_hash_entry *) h->root.u.i.link;
613 if (h->versioned == unknown)
615 /* Set versioned if symbol version is unknown. */
616 char *version = strrchr (name, ELF_VER_CHR);
619 if (version > name && version[-1] != ELF_VER_CHR)
620 h->versioned = versioned_hidden;
622 h->versioned = versioned;
626 /* Symbols defined in a linker script but not referenced anywhere
627 else will have non_elf set. */
630 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
634 switch (h->root.type)
636 case bfd_link_hash_defined:
637 case bfd_link_hash_defweak:
638 case bfd_link_hash_common:
640 case bfd_link_hash_undefweak:
641 case bfd_link_hash_undefined:
642 /* Since we're defining the symbol, don't let it seem to have not
643 been defined. record_dynamic_symbol and size_dynamic_sections
644 may depend on this. */
645 h->root.type = bfd_link_hash_new;
646 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
647 bfd_link_repair_undef_list (&htab->root);
649 case bfd_link_hash_new:
651 case bfd_link_hash_indirect:
652 /* We had a versioned symbol in a dynamic library. We make the
653 the versioned symbol point to this one. */
654 bed = get_elf_backend_data (output_bfd);
656 while (hv->root.type == bfd_link_hash_indirect
657 || hv->root.type == bfd_link_hash_warning)
658 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
659 /* We don't need to update h->root.u since linker will set them
661 h->root.type = bfd_link_hash_undefined;
662 hv->root.type = bfd_link_hash_indirect;
663 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
664 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
671 /* If this symbol is being provided by the linker script, and it is
672 currently defined by a dynamic object, but not by a regular
673 object, then mark it as undefined so that the generic linker will
674 force the correct value. */
678 h->root.type = bfd_link_hash_undefined;
680 /* If this symbol is not being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular object,
682 then clear out any version information because the symbol will not be
683 associated with the dynamic object any more. */
687 h->verinfo.verdef = NULL;
689 /* Make sure this symbol is not garbage collected. */
696 bed = get_elf_backend_data (output_bfd);
697 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
698 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
699 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
702 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
704 if (!bfd_link_relocatable (info)
706 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
707 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
712 || bfd_link_dll (info)
713 || elf_hash_table (info)->is_relocatable_executable)
716 if (! bfd_elf_link_record_dynamic_symbol (info, h))
719 /* If this is a weak defined symbol, and we know a corresponding
720 real symbol from the same dynamic object, make sure the real
721 symbol is also made into a dynamic symbol. */
722 if (h->u.weakdef != NULL
723 && h->u.weakdef->dynindx == -1)
725 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
733 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
734 success, and 2 on a failure caused by attempting to record a symbol
735 in a discarded section, eg. a discarded link-once section symbol. */
738 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
743 struct elf_link_local_dynamic_entry *entry;
744 struct elf_link_hash_table *eht;
745 struct elf_strtab_hash *dynstr;
748 Elf_External_Sym_Shndx eshndx;
749 char esym[sizeof (Elf64_External_Sym)];
751 if (! is_elf_hash_table (info->hash))
754 /* See if the entry exists already. */
755 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
756 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
759 amt = sizeof (*entry);
760 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
764 /* Go find the symbol, so that we can find it's name. */
765 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
766 1, input_indx, &entry->isym, esym, &eshndx))
768 bfd_release (input_bfd, entry);
772 if (entry->isym.st_shndx != SHN_UNDEF
773 && entry->isym.st_shndx < SHN_LORESERVE)
777 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
778 if (s == NULL || bfd_is_abs_section (s->output_section))
780 /* We can still bfd_release here as nothing has done another
781 bfd_alloc. We can't do this later in this function. */
782 bfd_release (input_bfd, entry);
787 name = (bfd_elf_string_from_elf_section
788 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
789 entry->isym.st_name));
791 dynstr = elf_hash_table (info)->dynstr;
794 /* Create a strtab to hold the dynamic symbol names. */
795 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
800 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
801 if (dynstr_index == (size_t) -1)
803 entry->isym.st_name = dynstr_index;
805 eht = elf_hash_table (info);
807 entry->next = eht->dynlocal;
808 eht->dynlocal = entry;
809 entry->input_bfd = input_bfd;
810 entry->input_indx = input_indx;
813 /* Whatever binding the symbol had before, it's now local. */
815 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
817 /* The dynindx will be set at the end of size_dynamic_sections. */
822 /* Return the dynindex of a local dynamic symbol. */
825 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
829 struct elf_link_local_dynamic_entry *e;
831 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
832 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
837 /* This function is used to renumber the dynamic symbols, if some of
838 them are removed because they are marked as local. This is called
839 via elf_link_hash_traverse. */
842 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
845 size_t *count = (size_t *) data;
850 if (h->dynindx != -1)
851 h->dynindx = ++(*count);
857 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
858 STB_LOCAL binding. */
861 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
864 size_t *count = (size_t *) data;
866 if (!h->forced_local)
869 if (h->dynindx != -1)
870 h->dynindx = ++(*count);
875 /* Return true if the dynamic symbol for a given section should be
876 omitted when creating a shared library. */
878 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
879 struct bfd_link_info *info,
882 struct elf_link_hash_table *htab;
885 switch (elf_section_data (p)->this_hdr.sh_type)
889 /* If sh_type is yet undecided, assume it could be
890 SHT_PROGBITS/SHT_NOBITS. */
892 htab = elf_hash_table (info);
893 if (p == htab->tls_sec)
896 if (htab->text_index_section != NULL)
897 return p != htab->text_index_section && p != htab->data_index_section;
899 return (htab->dynobj != NULL
900 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
901 && ip->output_section == p);
903 /* There shouldn't be section relative relocations
904 against any other section. */
910 /* Assign dynsym indices. In a shared library we generate a section
911 symbol for each output section, which come first. Next come symbols
912 which have been forced to local binding. Then all of the back-end
913 allocated local dynamic syms, followed by the rest of the global
917 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
918 struct bfd_link_info *info,
919 unsigned long *section_sym_count)
921 unsigned long dynsymcount = 0;
923 if (bfd_link_pic (info)
924 || elf_hash_table (info)->is_relocatable_executable)
926 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
928 for (p = output_bfd->sections; p ; p = p->next)
929 if ((p->flags & SEC_EXCLUDE) == 0
930 && (p->flags & SEC_ALLOC) != 0
931 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
932 elf_section_data (p)->dynindx = ++dynsymcount;
934 elf_section_data (p)->dynindx = 0;
936 *section_sym_count = dynsymcount;
938 elf_link_hash_traverse (elf_hash_table (info),
939 elf_link_renumber_local_hash_table_dynsyms,
942 if (elf_hash_table (info)->dynlocal)
944 struct elf_link_local_dynamic_entry *p;
945 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
946 p->dynindx = ++dynsymcount;
948 elf_hash_table (info)->local_dynsymcount = dynsymcount;
950 elf_link_hash_traverse (elf_hash_table (info),
951 elf_link_renumber_hash_table_dynsyms,
954 /* There is an unused NULL entry at the head of the table which we
955 must account for in our count even if the table is empty since it
956 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
960 elf_hash_table (info)->dynsymcount = dynsymcount;
964 /* Merge st_other field. */
967 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
968 const Elf_Internal_Sym *isym, asection *sec,
969 bfd_boolean definition, bfd_boolean dynamic)
971 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
973 /* If st_other has a processor-specific meaning, specific
974 code might be needed here. */
975 if (bed->elf_backend_merge_symbol_attribute)
976 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
981 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
982 unsigned hvis = ELF_ST_VISIBILITY (h->other);
984 /* Keep the most constraining visibility. Leave the remainder
985 of the st_other field to elf_backend_merge_symbol_attribute. */
986 if (symvis - 1 < hvis - 1)
987 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
990 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
991 && (sec->flags & SEC_READONLY) == 0)
992 h->protected_def = 1;
995 /* This function is called when we want to merge a new symbol with an
996 existing symbol. It handles the various cases which arise when we
997 find a definition in a dynamic object, or when there is already a
998 definition in a dynamic object. The new symbol is described by
999 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1000 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1001 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1002 of an old common symbol. We set OVERRIDE if the old symbol is
1003 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1004 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1005 to change. By OK to change, we mean that we shouldn't warn if the
1006 type or size does change. */
1009 _bfd_elf_merge_symbol (bfd *abfd,
1010 struct bfd_link_info *info,
1012 Elf_Internal_Sym *sym,
1015 struct elf_link_hash_entry **sym_hash,
1017 bfd_boolean *pold_weak,
1018 unsigned int *pold_alignment,
1020 bfd_boolean *override,
1021 bfd_boolean *type_change_ok,
1022 bfd_boolean *size_change_ok,
1023 bfd_boolean *matched)
1025 asection *sec, *oldsec;
1026 struct elf_link_hash_entry *h;
1027 struct elf_link_hash_entry *hi;
1028 struct elf_link_hash_entry *flip;
1031 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1032 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1033 const struct elf_backend_data *bed;
1040 bind = ELF_ST_BIND (sym->st_info);
1042 if (! bfd_is_und_section (sec))
1043 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1045 h = ((struct elf_link_hash_entry *)
1046 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1051 bed = get_elf_backend_data (abfd);
1053 /* NEW_VERSION is the symbol version of the new symbol. */
1054 if (h->versioned != unversioned)
1056 /* Symbol version is unknown or versioned. */
1057 new_version = strrchr (name, ELF_VER_CHR);
1060 if (h->versioned == unknown)
1062 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1063 h->versioned = versioned_hidden;
1065 h->versioned = versioned;
1068 if (new_version[0] == '\0')
1072 h->versioned = unversioned;
1077 /* For merging, we only care about real symbols. But we need to make
1078 sure that indirect symbol dynamic flags are updated. */
1080 while (h->root.type == bfd_link_hash_indirect
1081 || h->root.type == bfd_link_hash_warning)
1082 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1086 if (hi == h || h->root.type == bfd_link_hash_new)
1090 /* OLD_HIDDEN is true if the existing symbol is only visible
1091 to the symbol with the same symbol version. NEW_HIDDEN is
1092 true if the new symbol is only visible to the symbol with
1093 the same symbol version. */
1094 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1095 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1096 if (!old_hidden && !new_hidden)
1097 /* The new symbol matches the existing symbol if both
1102 /* OLD_VERSION is the symbol version of the existing
1106 if (h->versioned >= versioned)
1107 old_version = strrchr (h->root.root.string,
1112 /* The new symbol matches the existing symbol if they
1113 have the same symbol version. */
1114 *matched = (old_version == new_version
1115 || (old_version != NULL
1116 && new_version != NULL
1117 && strcmp (old_version, new_version) == 0));
1122 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1127 switch (h->root.type)
1132 case bfd_link_hash_undefined:
1133 case bfd_link_hash_undefweak:
1134 oldbfd = h->root.u.undef.abfd;
1137 case bfd_link_hash_defined:
1138 case bfd_link_hash_defweak:
1139 oldbfd = h->root.u.def.section->owner;
1140 oldsec = h->root.u.def.section;
1143 case bfd_link_hash_common:
1144 oldbfd = h->root.u.c.p->section->owner;
1145 oldsec = h->root.u.c.p->section;
1147 *pold_alignment = h->root.u.c.p->alignment_power;
1150 if (poldbfd && *poldbfd == NULL)
1153 /* Differentiate strong and weak symbols. */
1154 newweak = bind == STB_WEAK;
1155 oldweak = (h->root.type == bfd_link_hash_defweak
1156 || h->root.type == bfd_link_hash_undefweak);
1158 *pold_weak = oldweak;
1160 /* This code is for coping with dynamic objects, and is only useful
1161 if we are doing an ELF link. */
1162 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1165 /* We have to check it for every instance since the first few may be
1166 references and not all compilers emit symbol type for undefined
1168 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1170 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1171 respectively, is from a dynamic object. */
1173 newdyn = (abfd->flags & DYNAMIC) != 0;
1175 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1176 syms and defined syms in dynamic libraries respectively.
1177 ref_dynamic on the other hand can be set for a symbol defined in
1178 a dynamic library, and def_dynamic may not be set; When the
1179 definition in a dynamic lib is overridden by a definition in the
1180 executable use of the symbol in the dynamic lib becomes a
1181 reference to the executable symbol. */
1184 if (bfd_is_und_section (sec))
1186 if (bind != STB_WEAK)
1188 h->ref_dynamic_nonweak = 1;
1189 hi->ref_dynamic_nonweak = 1;
1194 /* Update the existing symbol only if they match. */
1197 hi->dynamic_def = 1;
1201 /* If we just created the symbol, mark it as being an ELF symbol.
1202 Other than that, there is nothing to do--there is no merge issue
1203 with a newly defined symbol--so we just return. */
1205 if (h->root.type == bfd_link_hash_new)
1211 /* In cases involving weak versioned symbols, we may wind up trying
1212 to merge a symbol with itself. Catch that here, to avoid the
1213 confusion that results if we try to override a symbol with
1214 itself. The additional tests catch cases like
1215 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1216 dynamic object, which we do want to handle here. */
1218 && (newweak || oldweak)
1219 && ((abfd->flags & DYNAMIC) == 0
1220 || !h->def_regular))
1225 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1226 else if (oldsec != NULL)
1228 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1229 indices used by MIPS ELF. */
1230 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1233 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1234 respectively, appear to be a definition rather than reference. */
1236 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1238 olddef = (h->root.type != bfd_link_hash_undefined
1239 && h->root.type != bfd_link_hash_undefweak
1240 && h->root.type != bfd_link_hash_common);
1242 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1243 respectively, appear to be a function. */
1245 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1246 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1248 oldfunc = (h->type != STT_NOTYPE
1249 && bed->is_function_type (h->type));
1251 if (!(newfunc && oldfunc)
1252 && ELF_ST_TYPE (sym->st_info) != h->type
1253 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1254 && h->type != STT_NOTYPE
1255 && (newdef || bfd_is_com_section (sec))
1256 && (olddef || h->root.type == bfd_link_hash_common))
1258 /* If creating a default indirect symbol ("foo" or "foo@") from
1259 a dynamic versioned definition ("foo@@") skip doing so if
1260 there is an existing regular definition with a different
1261 type. We don't want, for example, a "time" variable in the
1262 executable overriding a "time" function in a shared library. */
1270 /* When adding a symbol from a regular object file after we have
1271 created indirect symbols, undo the indirection and any
1278 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1279 h->forced_local = 0;
1283 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1285 h->root.type = bfd_link_hash_undefined;
1286 h->root.u.undef.abfd = abfd;
1290 h->root.type = bfd_link_hash_new;
1291 h->root.u.undef.abfd = NULL;
1297 /* Check TLS symbols. We don't check undefined symbols introduced
1298 by "ld -u" which have no type (and oldbfd NULL), and we don't
1299 check symbols from plugins because they also have no type. */
1301 && (oldbfd->flags & BFD_PLUGIN) == 0
1302 && (abfd->flags & BFD_PLUGIN) == 0
1303 && ELF_ST_TYPE (sym->st_info) != h->type
1304 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1307 bfd_boolean ntdef, tdef;
1308 asection *ntsec, *tsec;
1310 if (h->type == STT_TLS)
1331 /* xgettext:c-format */
1332 (_("%s: TLS definition in %B section %A "
1333 "mismatches non-TLS definition in %B section %A"),
1334 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1335 else if (!tdef && !ntdef)
1337 /* xgettext:c-format */
1338 (_("%s: TLS reference in %B "
1339 "mismatches non-TLS reference in %B"),
1340 h->root.root.string, tbfd, ntbfd);
1343 /* xgettext:c-format */
1344 (_("%s: TLS definition in %B section %A "
1345 "mismatches non-TLS reference in %B"),
1346 h->root.root.string, tbfd, tsec, ntbfd);
1349 /* xgettext:c-format */
1350 (_("%s: TLS reference in %B "
1351 "mismatches non-TLS definition in %B section %A"),
1352 h->root.root.string, tbfd, ntbfd, ntsec);
1354 bfd_set_error (bfd_error_bad_value);
1358 /* If the old symbol has non-default visibility, we ignore the new
1359 definition from a dynamic object. */
1361 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1362 && !bfd_is_und_section (sec))
1365 /* Make sure this symbol is dynamic. */
1367 hi->ref_dynamic = 1;
1368 /* A protected symbol has external availability. Make sure it is
1369 recorded as dynamic.
1371 FIXME: Should we check type and size for protected symbol? */
1372 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1373 return bfd_elf_link_record_dynamic_symbol (info, h);
1378 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1381 /* If the new symbol with non-default visibility comes from a
1382 relocatable file and the old definition comes from a dynamic
1383 object, we remove the old definition. */
1384 if (hi->root.type == bfd_link_hash_indirect)
1386 /* Handle the case where the old dynamic definition is
1387 default versioned. We need to copy the symbol info from
1388 the symbol with default version to the normal one if it
1389 was referenced before. */
1392 hi->root.type = h->root.type;
1393 h->root.type = bfd_link_hash_indirect;
1394 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1396 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1397 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1399 /* If the new symbol is hidden or internal, completely undo
1400 any dynamic link state. */
1401 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1402 h->forced_local = 0;
1409 /* FIXME: Should we check type and size for protected symbol? */
1419 /* If the old symbol was undefined before, then it will still be
1420 on the undefs list. If the new symbol is undefined or
1421 common, we can't make it bfd_link_hash_new here, because new
1422 undefined or common symbols will be added to the undefs list
1423 by _bfd_generic_link_add_one_symbol. Symbols may not be
1424 added twice to the undefs list. Also, if the new symbol is
1425 undefweak then we don't want to lose the strong undef. */
1426 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1428 h->root.type = bfd_link_hash_undefined;
1429 h->root.u.undef.abfd = abfd;
1433 h->root.type = bfd_link_hash_new;
1434 h->root.u.undef.abfd = NULL;
1437 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1439 /* If the new symbol is hidden or internal, completely undo
1440 any dynamic link state. */
1441 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1442 h->forced_local = 0;
1448 /* FIXME: Should we check type and size for protected symbol? */
1454 /* If a new weak symbol definition comes from a regular file and the
1455 old symbol comes from a dynamic library, we treat the new one as
1456 strong. Similarly, an old weak symbol definition from a regular
1457 file is treated as strong when the new symbol comes from a dynamic
1458 library. Further, an old weak symbol from a dynamic library is
1459 treated as strong if the new symbol is from a dynamic library.
1460 This reflects the way glibc's ld.so works.
1462 Do this before setting *type_change_ok or *size_change_ok so that
1463 we warn properly when dynamic library symbols are overridden. */
1465 if (newdef && !newdyn && olddyn)
1467 if (olddef && newdyn)
1470 /* Allow changes between different types of function symbol. */
1471 if (newfunc && oldfunc)
1472 *type_change_ok = TRUE;
1474 /* It's OK to change the type if either the existing symbol or the
1475 new symbol is weak. A type change is also OK if the old symbol
1476 is undefined and the new symbol is defined. */
1481 && h->root.type == bfd_link_hash_undefined))
1482 *type_change_ok = TRUE;
1484 /* It's OK to change the size if either the existing symbol or the
1485 new symbol is weak, or if the old symbol is undefined. */
1488 || h->root.type == bfd_link_hash_undefined)
1489 *size_change_ok = TRUE;
1491 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1492 symbol, respectively, appears to be a common symbol in a dynamic
1493 object. If a symbol appears in an uninitialized section, and is
1494 not weak, and is not a function, then it may be a common symbol
1495 which was resolved when the dynamic object was created. We want
1496 to treat such symbols specially, because they raise special
1497 considerations when setting the symbol size: if the symbol
1498 appears as a common symbol in a regular object, and the size in
1499 the regular object is larger, we must make sure that we use the
1500 larger size. This problematic case can always be avoided in C,
1501 but it must be handled correctly when using Fortran shared
1504 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1505 likewise for OLDDYNCOMMON and OLDDEF.
1507 Note that this test is just a heuristic, and that it is quite
1508 possible to have an uninitialized symbol in a shared object which
1509 is really a definition, rather than a common symbol. This could
1510 lead to some minor confusion when the symbol really is a common
1511 symbol in some regular object. However, I think it will be
1517 && (sec->flags & SEC_ALLOC) != 0
1518 && (sec->flags & SEC_LOAD) == 0
1521 newdyncommon = TRUE;
1523 newdyncommon = FALSE;
1527 && h->root.type == bfd_link_hash_defined
1529 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1530 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1533 olddyncommon = TRUE;
1535 olddyncommon = FALSE;
1537 /* We now know everything about the old and new symbols. We ask the
1538 backend to check if we can merge them. */
1539 if (bed->merge_symbol != NULL)
1541 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1546 /* If both the old and the new symbols look like common symbols in a
1547 dynamic object, set the size of the symbol to the larger of the
1552 && sym->st_size != h->size)
1554 /* Since we think we have two common symbols, issue a multiple
1555 common warning if desired. Note that we only warn if the
1556 size is different. If the size is the same, we simply let
1557 the old symbol override the new one as normally happens with
1558 symbols defined in dynamic objects. */
1560 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1561 bfd_link_hash_common, sym->st_size);
1562 if (sym->st_size > h->size)
1563 h->size = sym->st_size;
1565 *size_change_ok = TRUE;
1568 /* If we are looking at a dynamic object, and we have found a
1569 definition, we need to see if the symbol was already defined by
1570 some other object. If so, we want to use the existing
1571 definition, and we do not want to report a multiple symbol
1572 definition error; we do this by clobbering *PSEC to be
1573 bfd_und_section_ptr.
1575 We treat a common symbol as a definition if the symbol in the
1576 shared library is a function, since common symbols always
1577 represent variables; this can cause confusion in principle, but
1578 any such confusion would seem to indicate an erroneous program or
1579 shared library. We also permit a common symbol in a regular
1580 object to override a weak symbol in a shared object. */
1585 || (h->root.type == bfd_link_hash_common
1586 && (newweak || newfunc))))
1590 newdyncommon = FALSE;
1592 *psec = sec = bfd_und_section_ptr;
1593 *size_change_ok = TRUE;
1595 /* If we get here when the old symbol is a common symbol, then
1596 we are explicitly letting it override a weak symbol or
1597 function in a dynamic object, and we don't want to warn about
1598 a type change. If the old symbol is a defined symbol, a type
1599 change warning may still be appropriate. */
1601 if (h->root.type == bfd_link_hash_common)
1602 *type_change_ok = TRUE;
1605 /* Handle the special case of an old common symbol merging with a
1606 new symbol which looks like a common symbol in a shared object.
1607 We change *PSEC and *PVALUE to make the new symbol look like a
1608 common symbol, and let _bfd_generic_link_add_one_symbol do the
1612 && h->root.type == bfd_link_hash_common)
1616 newdyncommon = FALSE;
1617 *pvalue = sym->st_size;
1618 *psec = sec = bed->common_section (oldsec);
1619 *size_change_ok = TRUE;
1622 /* Skip weak definitions of symbols that are already defined. */
1623 if (newdef && olddef && newweak)
1625 /* Don't skip new non-IR weak syms. */
1626 if (!(oldbfd != NULL
1627 && (oldbfd->flags & BFD_PLUGIN) != 0
1628 && (abfd->flags & BFD_PLUGIN) == 0))
1634 /* Merge st_other. If the symbol already has a dynamic index,
1635 but visibility says it should not be visible, turn it into a
1637 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1638 if (h->dynindx != -1)
1639 switch (ELF_ST_VISIBILITY (h->other))
1643 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1648 /* If the old symbol is from a dynamic object, and the new symbol is
1649 a definition which is not from a dynamic object, then the new
1650 symbol overrides the old symbol. Symbols from regular files
1651 always take precedence over symbols from dynamic objects, even if
1652 they are defined after the dynamic object in the link.
1654 As above, we again permit a common symbol in a regular object to
1655 override a definition in a shared object if the shared object
1656 symbol is a function or is weak. */
1661 || (bfd_is_com_section (sec)
1662 && (oldweak || oldfunc)))
1667 /* Change the hash table entry to undefined, and let
1668 _bfd_generic_link_add_one_symbol do the right thing with the
1671 h->root.type = bfd_link_hash_undefined;
1672 h->root.u.undef.abfd = h->root.u.def.section->owner;
1673 *size_change_ok = TRUE;
1676 olddyncommon = FALSE;
1678 /* We again permit a type change when a common symbol may be
1679 overriding a function. */
1681 if (bfd_is_com_section (sec))
1685 /* If a common symbol overrides a function, make sure
1686 that it isn't defined dynamically nor has type
1689 h->type = STT_NOTYPE;
1691 *type_change_ok = TRUE;
1694 if (hi->root.type == bfd_link_hash_indirect)
1697 /* This union may have been set to be non-NULL when this symbol
1698 was seen in a dynamic object. We must force the union to be
1699 NULL, so that it is correct for a regular symbol. */
1700 h->verinfo.vertree = NULL;
1703 /* Handle the special case of a new common symbol merging with an
1704 old symbol that looks like it might be a common symbol defined in
1705 a shared object. Note that we have already handled the case in
1706 which a new common symbol should simply override the definition
1707 in the shared library. */
1710 && bfd_is_com_section (sec)
1713 /* It would be best if we could set the hash table entry to a
1714 common symbol, but we don't know what to use for the section
1715 or the alignment. */
1716 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1717 bfd_link_hash_common, sym->st_size);
1719 /* If the presumed common symbol in the dynamic object is
1720 larger, pretend that the new symbol has its size. */
1722 if (h->size > *pvalue)
1725 /* We need to remember the alignment required by the symbol
1726 in the dynamic object. */
1727 BFD_ASSERT (pold_alignment);
1728 *pold_alignment = h->root.u.def.section->alignment_power;
1731 olddyncommon = FALSE;
1733 h->root.type = bfd_link_hash_undefined;
1734 h->root.u.undef.abfd = h->root.u.def.section->owner;
1736 *size_change_ok = TRUE;
1737 *type_change_ok = TRUE;
1739 if (hi->root.type == bfd_link_hash_indirect)
1742 h->verinfo.vertree = NULL;
1747 /* Handle the case where we had a versioned symbol in a dynamic
1748 library and now find a definition in a normal object. In this
1749 case, we make the versioned symbol point to the normal one. */
1750 flip->root.type = h->root.type;
1751 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1752 h->root.type = bfd_link_hash_indirect;
1753 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1754 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1758 flip->ref_dynamic = 1;
1765 /* This function is called to create an indirect symbol from the
1766 default for the symbol with the default version if needed. The
1767 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1768 set DYNSYM if the new indirect symbol is dynamic. */
1771 _bfd_elf_add_default_symbol (bfd *abfd,
1772 struct bfd_link_info *info,
1773 struct elf_link_hash_entry *h,
1775 Elf_Internal_Sym *sym,
1779 bfd_boolean *dynsym)
1781 bfd_boolean type_change_ok;
1782 bfd_boolean size_change_ok;
1785 struct elf_link_hash_entry *hi;
1786 struct bfd_link_hash_entry *bh;
1787 const struct elf_backend_data *bed;
1788 bfd_boolean collect;
1789 bfd_boolean dynamic;
1790 bfd_boolean override;
1792 size_t len, shortlen;
1794 bfd_boolean matched;
1796 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1799 /* If this symbol has a version, and it is the default version, we
1800 create an indirect symbol from the default name to the fully
1801 decorated name. This will cause external references which do not
1802 specify a version to be bound to this version of the symbol. */
1803 p = strchr (name, ELF_VER_CHR);
1804 if (h->versioned == unknown)
1808 h->versioned = unversioned;
1813 if (p[1] != ELF_VER_CHR)
1815 h->versioned = versioned_hidden;
1819 h->versioned = versioned;
1824 /* PR ld/19073: We may see an unversioned definition after the
1830 bed = get_elf_backend_data (abfd);
1831 collect = bed->collect;
1832 dynamic = (abfd->flags & DYNAMIC) != 0;
1834 shortlen = p - name;
1835 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1836 if (shortname == NULL)
1838 memcpy (shortname, name, shortlen);
1839 shortname[shortlen] = '\0';
1841 /* We are going to create a new symbol. Merge it with any existing
1842 symbol with this name. For the purposes of the merge, act as
1843 though we were defining the symbol we just defined, although we
1844 actually going to define an indirect symbol. */
1845 type_change_ok = FALSE;
1846 size_change_ok = FALSE;
1849 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1850 &hi, poldbfd, NULL, NULL, &skip, &override,
1851 &type_change_ok, &size_change_ok, &matched))
1857 if (hi->def_regular)
1859 /* If the undecorated symbol will have a version added by a
1860 script different to H, then don't indirect to/from the
1861 undecorated symbol. This isn't ideal because we may not yet
1862 have seen symbol versions, if given by a script on the
1863 command line rather than via --version-script. */
1864 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1869 = bfd_find_version_for_sym (info->version_info,
1870 hi->root.root.string, &hide);
1871 if (hi->verinfo.vertree != NULL && hide)
1873 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1877 if (hi->verinfo.vertree != NULL
1878 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1884 /* Add the default symbol if not performing a relocatable link. */
1885 if (! bfd_link_relocatable (info))
1888 if (! (_bfd_generic_link_add_one_symbol
1889 (info, abfd, shortname, BSF_INDIRECT,
1890 bfd_ind_section_ptr,
1891 0, name, FALSE, collect, &bh)))
1893 hi = (struct elf_link_hash_entry *) bh;
1898 /* In this case the symbol named SHORTNAME is overriding the
1899 indirect symbol we want to add. We were planning on making
1900 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1901 is the name without a version. NAME is the fully versioned
1902 name, and it is the default version.
1904 Overriding means that we already saw a definition for the
1905 symbol SHORTNAME in a regular object, and it is overriding
1906 the symbol defined in the dynamic object.
1908 When this happens, we actually want to change NAME, the
1909 symbol we just added, to refer to SHORTNAME. This will cause
1910 references to NAME in the shared object to become references
1911 to SHORTNAME in the regular object. This is what we expect
1912 when we override a function in a shared object: that the
1913 references in the shared object will be mapped to the
1914 definition in the regular object. */
1916 while (hi->root.type == bfd_link_hash_indirect
1917 || hi->root.type == bfd_link_hash_warning)
1918 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1920 h->root.type = bfd_link_hash_indirect;
1921 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1925 hi->ref_dynamic = 1;
1929 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1934 /* Now set HI to H, so that the following code will set the
1935 other fields correctly. */
1939 /* Check if HI is a warning symbol. */
1940 if (hi->root.type == bfd_link_hash_warning)
1941 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1943 /* If there is a duplicate definition somewhere, then HI may not
1944 point to an indirect symbol. We will have reported an error to
1945 the user in that case. */
1947 if (hi->root.type == bfd_link_hash_indirect)
1949 struct elf_link_hash_entry *ht;
1951 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1952 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1954 /* A reference to the SHORTNAME symbol from a dynamic library
1955 will be satisfied by the versioned symbol at runtime. In
1956 effect, we have a reference to the versioned symbol. */
1957 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1958 hi->dynamic_def |= ht->dynamic_def;
1960 /* See if the new flags lead us to realize that the symbol must
1966 if (! bfd_link_executable (info)
1973 if (hi->ref_regular)
1979 /* We also need to define an indirection from the nondefault version
1983 len = strlen (name);
1984 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1985 if (shortname == NULL)
1987 memcpy (shortname, name, shortlen);
1988 memcpy (shortname + shortlen, p + 1, len - shortlen);
1990 /* Once again, merge with any existing symbol. */
1991 type_change_ok = FALSE;
1992 size_change_ok = FALSE;
1994 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1995 &hi, poldbfd, NULL, NULL, &skip, &override,
1996 &type_change_ok, &size_change_ok, &matched))
2004 /* Here SHORTNAME is a versioned name, so we don't expect to see
2005 the type of override we do in the case above unless it is
2006 overridden by a versioned definition. */
2007 if (hi->root.type != bfd_link_hash_defined
2008 && hi->root.type != bfd_link_hash_defweak)
2010 /* xgettext:c-format */
2011 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2017 if (! (_bfd_generic_link_add_one_symbol
2018 (info, abfd, shortname, BSF_INDIRECT,
2019 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2021 hi = (struct elf_link_hash_entry *) bh;
2023 /* If there is a duplicate definition somewhere, then HI may not
2024 point to an indirect symbol. We will have reported an error
2025 to the user in that case. */
2027 if (hi->root.type == bfd_link_hash_indirect)
2029 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2030 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2031 hi->dynamic_def |= h->dynamic_def;
2033 /* See if the new flags lead us to realize that the symbol
2039 if (! bfd_link_executable (info)
2045 if (hi->ref_regular)
2055 /* This routine is used to export all defined symbols into the dynamic
2056 symbol table. It is called via elf_link_hash_traverse. */
2059 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2061 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2063 /* Ignore indirect symbols. These are added by the versioning code. */
2064 if (h->root.type == bfd_link_hash_indirect)
2067 /* Ignore this if we won't export it. */
2068 if (!eif->info->export_dynamic && !h->dynamic)
2071 if (h->dynindx == -1
2072 && (h->def_regular || h->ref_regular)
2073 && ! bfd_hide_sym_by_version (eif->info->version_info,
2074 h->root.root.string))
2076 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2086 /* Look through the symbols which are defined in other shared
2087 libraries and referenced here. Update the list of version
2088 dependencies. This will be put into the .gnu.version_r section.
2089 This function is called via elf_link_hash_traverse. */
2092 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2095 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2096 Elf_Internal_Verneed *t;
2097 Elf_Internal_Vernaux *a;
2100 /* We only care about symbols defined in shared objects with version
2105 || h->verinfo.verdef == NULL
2106 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2107 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2110 /* See if we already know about this version. */
2111 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2115 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2118 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2119 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2125 /* This is a new version. Add it to tree we are building. */
2130 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2133 rinfo->failed = TRUE;
2137 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2138 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2139 elf_tdata (rinfo->info->output_bfd)->verref = t;
2143 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2146 rinfo->failed = TRUE;
2150 /* Note that we are copying a string pointer here, and testing it
2151 above. If bfd_elf_string_from_elf_section is ever changed to
2152 discard the string data when low in memory, this will have to be
2154 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2156 a->vna_flags = h->verinfo.verdef->vd_flags;
2157 a->vna_nextptr = t->vn_auxptr;
2159 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2162 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2169 /* Figure out appropriate versions for all the symbols. We may not
2170 have the version number script until we have read all of the input
2171 files, so until that point we don't know which symbols should be
2172 local. This function is called via elf_link_hash_traverse. */
2175 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2177 struct elf_info_failed *sinfo;
2178 struct bfd_link_info *info;
2179 const struct elf_backend_data *bed;
2180 struct elf_info_failed eif;
2183 sinfo = (struct elf_info_failed *) data;
2186 /* Fix the symbol flags. */
2189 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2192 sinfo->failed = TRUE;
2196 /* We only need version numbers for symbols defined in regular
2198 if (!h->def_regular)
2201 bed = get_elf_backend_data (info->output_bfd);
2202 p = strchr (h->root.root.string, ELF_VER_CHR);
2203 if (p != NULL && h->verinfo.vertree == NULL)
2205 struct bfd_elf_version_tree *t;
2208 if (*p == ELF_VER_CHR)
2211 /* If there is no version string, we can just return out. */
2215 /* Look for the version. If we find it, it is no longer weak. */
2216 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2218 if (strcmp (t->name, p) == 0)
2222 struct bfd_elf_version_expr *d;
2224 len = p - h->root.root.string;
2225 alc = (char *) bfd_malloc (len);
2228 sinfo->failed = TRUE;
2231 memcpy (alc, h->root.root.string, len - 1);
2232 alc[len - 1] = '\0';
2233 if (alc[len - 2] == ELF_VER_CHR)
2234 alc[len - 2] = '\0';
2236 h->verinfo.vertree = t;
2240 if (t->globals.list != NULL)
2241 d = (*t->match) (&t->globals, NULL, alc);
2243 /* See if there is anything to force this symbol to
2245 if (d == NULL && t->locals.list != NULL)
2247 d = (*t->match) (&t->locals, NULL, alc);
2250 && ! info->export_dynamic)
2251 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2259 /* If we are building an application, we need to create a
2260 version node for this version. */
2261 if (t == NULL && bfd_link_executable (info))
2263 struct bfd_elf_version_tree **pp;
2266 /* If we aren't going to export this symbol, we don't need
2267 to worry about it. */
2268 if (h->dynindx == -1)
2271 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2275 sinfo->failed = TRUE;
2280 t->name_indx = (unsigned int) -1;
2284 /* Don't count anonymous version tag. */
2285 if (sinfo->info->version_info != NULL
2286 && sinfo->info->version_info->vernum == 0)
2288 for (pp = &sinfo->info->version_info;
2292 t->vernum = version_index;
2296 h->verinfo.vertree = t;
2300 /* We could not find the version for a symbol when
2301 generating a shared archive. Return an error. */
2303 /* xgettext:c-format */
2304 (_("%B: version node not found for symbol %s"),
2305 info->output_bfd, h->root.root.string);
2306 bfd_set_error (bfd_error_bad_value);
2307 sinfo->failed = TRUE;
2312 /* If we don't have a version for this symbol, see if we can find
2314 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2319 = bfd_find_version_for_sym (sinfo->info->version_info,
2320 h->root.root.string, &hide);
2321 if (h->verinfo.vertree != NULL && hide)
2322 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2328 /* Read and swap the relocs from the section indicated by SHDR. This
2329 may be either a REL or a RELA section. The relocations are
2330 translated into RELA relocations and stored in INTERNAL_RELOCS,
2331 which should have already been allocated to contain enough space.
2332 The EXTERNAL_RELOCS are a buffer where the external form of the
2333 relocations should be stored.
2335 Returns FALSE if something goes wrong. */
2338 elf_link_read_relocs_from_section (bfd *abfd,
2340 Elf_Internal_Shdr *shdr,
2341 void *external_relocs,
2342 Elf_Internal_Rela *internal_relocs)
2344 const struct elf_backend_data *bed;
2345 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2346 const bfd_byte *erela;
2347 const bfd_byte *erelaend;
2348 Elf_Internal_Rela *irela;
2349 Elf_Internal_Shdr *symtab_hdr;
2352 /* Position ourselves at the start of the section. */
2353 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2356 /* Read the relocations. */
2357 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2360 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2361 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2363 bed = get_elf_backend_data (abfd);
2365 /* Convert the external relocations to the internal format. */
2366 if (shdr->sh_entsize == bed->s->sizeof_rel)
2367 swap_in = bed->s->swap_reloc_in;
2368 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2369 swap_in = bed->s->swap_reloca_in;
2372 bfd_set_error (bfd_error_wrong_format);
2376 erela = (const bfd_byte *) external_relocs;
2377 erelaend = erela + shdr->sh_size;
2378 irela = internal_relocs;
2379 while (erela < erelaend)
2383 (*swap_in) (abfd, erela, irela);
2384 r_symndx = ELF32_R_SYM (irela->r_info);
2385 if (bed->s->arch_size == 64)
2389 if ((size_t) r_symndx >= nsyms)
2392 /* xgettext:c-format */
2393 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2394 " for offset 0x%lx in section `%A'"),
2395 abfd, (unsigned long) r_symndx, (unsigned long) nsyms,
2396 irela->r_offset, sec);
2397 bfd_set_error (bfd_error_bad_value);
2401 else if (r_symndx != STN_UNDEF)
2404 /* xgettext:c-format */
2405 (_("%B: non-zero symbol index (0x%lx)"
2406 " for offset 0x%lx in section `%A'"
2407 " when the object file has no symbol table"),
2408 abfd, (unsigned long) r_symndx, (unsigned long) nsyms,
2409 irela->r_offset, sec);
2410 bfd_set_error (bfd_error_bad_value);
2413 irela += bed->s->int_rels_per_ext_rel;
2414 erela += shdr->sh_entsize;
2420 /* Read and swap the relocs for a section O. They may have been
2421 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2422 not NULL, they are used as buffers to read into. They are known to
2423 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2424 the return value is allocated using either malloc or bfd_alloc,
2425 according to the KEEP_MEMORY argument. If O has two relocation
2426 sections (both REL and RELA relocations), then the REL_HDR
2427 relocations will appear first in INTERNAL_RELOCS, followed by the
2428 RELA_HDR relocations. */
2431 _bfd_elf_link_read_relocs (bfd *abfd,
2433 void *external_relocs,
2434 Elf_Internal_Rela *internal_relocs,
2435 bfd_boolean keep_memory)
2437 void *alloc1 = NULL;
2438 Elf_Internal_Rela *alloc2 = NULL;
2439 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2440 struct bfd_elf_section_data *esdo = elf_section_data (o);
2441 Elf_Internal_Rela *internal_rela_relocs;
2443 if (esdo->relocs != NULL)
2444 return esdo->relocs;
2446 if (o->reloc_count == 0)
2449 if (internal_relocs == NULL)
2453 size = o->reloc_count;
2454 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2456 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2458 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2459 if (internal_relocs == NULL)
2463 if (external_relocs == NULL)
2465 bfd_size_type size = 0;
2468 size += esdo->rel.hdr->sh_size;
2470 size += esdo->rela.hdr->sh_size;
2472 alloc1 = bfd_malloc (size);
2475 external_relocs = alloc1;
2478 internal_rela_relocs = internal_relocs;
2481 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2485 external_relocs = (((bfd_byte *) external_relocs)
2486 + esdo->rel.hdr->sh_size);
2487 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2488 * bed->s->int_rels_per_ext_rel);
2492 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2494 internal_rela_relocs)))
2497 /* Cache the results for next time, if we can. */
2499 esdo->relocs = internal_relocs;
2504 /* Don't free alloc2, since if it was allocated we are passing it
2505 back (under the name of internal_relocs). */
2507 return internal_relocs;
2515 bfd_release (abfd, alloc2);
2522 /* Compute the size of, and allocate space for, REL_HDR which is the
2523 section header for a section containing relocations for O. */
2526 _bfd_elf_link_size_reloc_section (bfd *abfd,
2527 struct bfd_elf_section_reloc_data *reldata)
2529 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2531 /* That allows us to calculate the size of the section. */
2532 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2534 /* The contents field must last into write_object_contents, so we
2535 allocate it with bfd_alloc rather than malloc. Also since we
2536 cannot be sure that the contents will actually be filled in,
2537 we zero the allocated space. */
2538 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2539 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2542 if (reldata->hashes == NULL && reldata->count)
2544 struct elf_link_hash_entry **p;
2546 p = ((struct elf_link_hash_entry **)
2547 bfd_zmalloc (reldata->count * sizeof (*p)));
2551 reldata->hashes = p;
2557 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2558 originated from the section given by INPUT_REL_HDR) to the
2562 _bfd_elf_link_output_relocs (bfd *output_bfd,
2563 asection *input_section,
2564 Elf_Internal_Shdr *input_rel_hdr,
2565 Elf_Internal_Rela *internal_relocs,
2566 struct elf_link_hash_entry **rel_hash
2569 Elf_Internal_Rela *irela;
2570 Elf_Internal_Rela *irelaend;
2572 struct bfd_elf_section_reloc_data *output_reldata;
2573 asection *output_section;
2574 const struct elf_backend_data *bed;
2575 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2576 struct bfd_elf_section_data *esdo;
2578 output_section = input_section->output_section;
2580 bed = get_elf_backend_data (output_bfd);
2581 esdo = elf_section_data (output_section);
2582 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2584 output_reldata = &esdo->rel;
2585 swap_out = bed->s->swap_reloc_out;
2587 else if (esdo->rela.hdr
2588 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2590 output_reldata = &esdo->rela;
2591 swap_out = bed->s->swap_reloca_out;
2596 /* xgettext:c-format */
2597 (_("%B: relocation size mismatch in %B section %A"),
2598 output_bfd, input_section->owner, input_section);
2599 bfd_set_error (bfd_error_wrong_format);
2603 erel = output_reldata->hdr->contents;
2604 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2605 irela = internal_relocs;
2606 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2607 * bed->s->int_rels_per_ext_rel);
2608 while (irela < irelaend)
2610 (*swap_out) (output_bfd, irela, erel);
2611 irela += bed->s->int_rels_per_ext_rel;
2612 erel += input_rel_hdr->sh_entsize;
2615 /* Bump the counter, so that we know where to add the next set of
2617 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2622 /* Make weak undefined symbols in PIE dynamic. */
2625 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2626 struct elf_link_hash_entry *h)
2628 if (bfd_link_pie (info)
2630 && h->root.type == bfd_link_hash_undefweak)
2631 return bfd_elf_link_record_dynamic_symbol (info, h);
2636 /* Fix up the flags for a symbol. This handles various cases which
2637 can only be fixed after all the input files are seen. This is
2638 currently called by both adjust_dynamic_symbol and
2639 assign_sym_version, which is unnecessary but perhaps more robust in
2640 the face of future changes. */
2643 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2644 struct elf_info_failed *eif)
2646 const struct elf_backend_data *bed;
2648 /* If this symbol was mentioned in a non-ELF file, try to set
2649 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2650 permit a non-ELF file to correctly refer to a symbol defined in
2651 an ELF dynamic object. */
2654 while (h->root.type == bfd_link_hash_indirect)
2655 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2657 if (h->root.type != bfd_link_hash_defined
2658 && h->root.type != bfd_link_hash_defweak)
2661 h->ref_regular_nonweak = 1;
2665 if (h->root.u.def.section->owner != NULL
2666 && (bfd_get_flavour (h->root.u.def.section->owner)
2667 == bfd_target_elf_flavour))
2670 h->ref_regular_nonweak = 1;
2676 if (h->dynindx == -1
2680 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2689 /* Unfortunately, NON_ELF is only correct if the symbol
2690 was first seen in a non-ELF file. Fortunately, if the symbol
2691 was first seen in an ELF file, we're probably OK unless the
2692 symbol was defined in a non-ELF file. Catch that case here.
2693 FIXME: We're still in trouble if the symbol was first seen in
2694 a dynamic object, and then later in a non-ELF regular object. */
2695 if ((h->root.type == bfd_link_hash_defined
2696 || h->root.type == bfd_link_hash_defweak)
2698 && (h->root.u.def.section->owner != NULL
2699 ? (bfd_get_flavour (h->root.u.def.section->owner)
2700 != bfd_target_elf_flavour)
2701 : (bfd_is_abs_section (h->root.u.def.section)
2702 && !h->def_dynamic)))
2706 /* Backend specific symbol fixup. */
2707 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2708 if (bed->elf_backend_fixup_symbol
2709 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2712 /* If this is a final link, and the symbol was defined as a common
2713 symbol in a regular object file, and there was no definition in
2714 any dynamic object, then the linker will have allocated space for
2715 the symbol in a common section but the DEF_REGULAR
2716 flag will not have been set. */
2717 if (h->root.type == bfd_link_hash_defined
2721 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2724 /* If a weak undefined symbol has non-default visibility, we also
2725 hide it from the dynamic linker. */
2726 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2727 && h->root.type == bfd_link_hash_undefweak)
2728 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2730 /* A hidden versioned symbol in executable should be forced local if
2731 it is is locally defined, not referenced by shared library and not
2733 else if (bfd_link_executable (eif->info)
2734 && h->versioned == versioned_hidden
2735 && !eif->info->export_dynamic
2739 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2741 /* If -Bsymbolic was used (which means to bind references to global
2742 symbols to the definition within the shared object), and this
2743 symbol was defined in a regular object, then it actually doesn't
2744 need a PLT entry. Likewise, if the symbol has non-default
2745 visibility. If the symbol has hidden or internal visibility, we
2746 will force it local. */
2747 else if (h->needs_plt
2748 && bfd_link_pic (eif->info)
2749 && is_elf_hash_table (eif->info->hash)
2750 && (SYMBOLIC_BIND (eif->info, h)
2751 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2754 bfd_boolean force_local;
2756 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2757 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2758 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2761 /* If this is a weak defined symbol in a dynamic object, and we know
2762 the real definition in the dynamic object, copy interesting flags
2763 over to the real definition. */
2764 if (h->u.weakdef != NULL)
2766 /* If the real definition is defined by a regular object file,
2767 don't do anything special. See the longer description in
2768 _bfd_elf_adjust_dynamic_symbol, below. */
2769 if (h->u.weakdef->def_regular)
2770 h->u.weakdef = NULL;
2773 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2775 while (h->root.type == bfd_link_hash_indirect)
2776 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2778 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2779 || h->root.type == bfd_link_hash_defweak);
2780 BFD_ASSERT (weakdef->def_dynamic);
2781 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2782 || weakdef->root.type == bfd_link_hash_defweak);
2783 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2790 /* Make the backend pick a good value for a dynamic symbol. This is
2791 called via elf_link_hash_traverse, and also calls itself
2795 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2797 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2799 const struct elf_backend_data *bed;
2801 if (! is_elf_hash_table (eif->info->hash))
2804 /* Ignore indirect symbols. These are added by the versioning code. */
2805 if (h->root.type == bfd_link_hash_indirect)
2808 /* Fix the symbol flags. */
2809 if (! _bfd_elf_fix_symbol_flags (h, eif))
2812 if (h->root.type == bfd_link_hash_undefweak)
2814 if (eif->info->dynamic_undefined_weak == 0)
2815 _bfd_elf_link_hash_hide_symbol (eif->info, h, TRUE);
2816 else if (eif->info->dynamic_undefined_weak > 0
2818 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2819 && !bfd_hide_sym_by_version (eif->info->version_info,
2820 h->root.root.string))
2822 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2830 /* If this symbol does not require a PLT entry, and it is not
2831 defined by a dynamic object, or is not referenced by a regular
2832 object, ignore it. We do have to handle a weak defined symbol,
2833 even if no regular object refers to it, if we decided to add it
2834 to the dynamic symbol table. FIXME: Do we normally need to worry
2835 about symbols which are defined by one dynamic object and
2836 referenced by another one? */
2838 && h->type != STT_GNU_IFUNC
2842 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2844 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2848 /* If we've already adjusted this symbol, don't do it again. This
2849 can happen via a recursive call. */
2850 if (h->dynamic_adjusted)
2853 /* Don't look at this symbol again. Note that we must set this
2854 after checking the above conditions, because we may look at a
2855 symbol once, decide not to do anything, and then get called
2856 recursively later after REF_REGULAR is set below. */
2857 h->dynamic_adjusted = 1;
2859 /* If this is a weak definition, and we know a real definition, and
2860 the real symbol is not itself defined by a regular object file,
2861 then get a good value for the real definition. We handle the
2862 real symbol first, for the convenience of the backend routine.
2864 Note that there is a confusing case here. If the real definition
2865 is defined by a regular object file, we don't get the real symbol
2866 from the dynamic object, but we do get the weak symbol. If the
2867 processor backend uses a COPY reloc, then if some routine in the
2868 dynamic object changes the real symbol, we will not see that
2869 change in the corresponding weak symbol. This is the way other
2870 ELF linkers work as well, and seems to be a result of the shared
2873 I will clarify this issue. Most SVR4 shared libraries define the
2874 variable _timezone and define timezone as a weak synonym. The
2875 tzset call changes _timezone. If you write
2876 extern int timezone;
2878 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2879 you might expect that, since timezone is a synonym for _timezone,
2880 the same number will print both times. However, if the processor
2881 backend uses a COPY reloc, then actually timezone will be copied
2882 into your process image, and, since you define _timezone
2883 yourself, _timezone will not. Thus timezone and _timezone will
2884 wind up at different memory locations. The tzset call will set
2885 _timezone, leaving timezone unchanged. */
2887 if (h->u.weakdef != NULL)
2889 /* If we get to this point, there is an implicit reference to
2890 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2891 h->u.weakdef->ref_regular = 1;
2893 /* Ensure that the backend adjust_dynamic_symbol function sees
2894 H->U.WEAKDEF before H by recursively calling ourselves. */
2895 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2899 /* If a symbol has no type and no size and does not require a PLT
2900 entry, then we are probably about to do the wrong thing here: we
2901 are probably going to create a COPY reloc for an empty object.
2902 This case can arise when a shared object is built with assembly
2903 code, and the assembly code fails to set the symbol type. */
2905 && h->type == STT_NOTYPE
2908 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2909 h->root.root.string);
2911 dynobj = elf_hash_table (eif->info)->dynobj;
2912 bed = get_elf_backend_data (dynobj);
2914 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2923 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2927 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2928 struct elf_link_hash_entry *h,
2931 unsigned int power_of_two;
2933 asection *sec = h->root.u.def.section;
2935 /* The section aligment of definition is the maximum alignment
2936 requirement of symbols defined in the section. Since we don't
2937 know the symbol alignment requirement, we start with the
2938 maximum alignment and check low bits of the symbol address
2939 for the minimum alignment. */
2940 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2941 mask = ((bfd_vma) 1 << power_of_two) - 1;
2942 while ((h->root.u.def.value & mask) != 0)
2948 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2951 /* Adjust the section alignment if needed. */
2952 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2957 /* We make sure that the symbol will be aligned properly. */
2958 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2960 /* Define the symbol as being at this point in DYNBSS. */
2961 h->root.u.def.section = dynbss;
2962 h->root.u.def.value = dynbss->size;
2964 /* Increment the size of DYNBSS to make room for the symbol. */
2965 dynbss->size += h->size;
2967 /* No error if extern_protected_data is true. */
2968 if (h->protected_def
2969 && (!info->extern_protected_data
2970 || (info->extern_protected_data < 0
2971 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2972 info->callbacks->einfo
2973 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2974 h->root.root.string);
2979 /* Adjust all external symbols pointing into SEC_MERGE sections
2980 to reflect the object merging within the sections. */
2983 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2987 if ((h->root.type == bfd_link_hash_defined
2988 || h->root.type == bfd_link_hash_defweak)
2989 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2990 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2992 bfd *output_bfd = (bfd *) data;
2994 h->root.u.def.value =
2995 _bfd_merged_section_offset (output_bfd,
2996 &h->root.u.def.section,
2997 elf_section_data (sec)->sec_info,
2998 h->root.u.def.value);
3004 /* Returns false if the symbol referred to by H should be considered
3005 to resolve local to the current module, and true if it should be
3006 considered to bind dynamically. */
3009 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3010 struct bfd_link_info *info,
3011 bfd_boolean not_local_protected)
3013 bfd_boolean binding_stays_local_p;
3014 const struct elf_backend_data *bed;
3015 struct elf_link_hash_table *hash_table;
3020 while (h->root.type == bfd_link_hash_indirect
3021 || h->root.type == bfd_link_hash_warning)
3022 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3024 /* If it was forced local, then clearly it's not dynamic. */
3025 if (h->dynindx == -1)
3027 if (h->forced_local)
3030 /* Identify the cases where name binding rules say that a
3031 visible symbol resolves locally. */
3032 binding_stays_local_p = (bfd_link_executable (info)
3033 || SYMBOLIC_BIND (info, h));
3035 switch (ELF_ST_VISIBILITY (h->other))
3042 hash_table = elf_hash_table (info);
3043 if (!is_elf_hash_table (hash_table))
3046 bed = get_elf_backend_data (hash_table->dynobj);
3048 /* Proper resolution for function pointer equality may require
3049 that these symbols perhaps be resolved dynamically, even though
3050 we should be resolving them to the current module. */
3051 if (!not_local_protected || !bed->is_function_type (h->type))
3052 binding_stays_local_p = TRUE;
3059 /* If it isn't defined locally, then clearly it's dynamic. */
3060 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3063 /* Otherwise, the symbol is dynamic if binding rules don't tell
3064 us that it remains local. */
3065 return !binding_stays_local_p;
3068 /* Return true if the symbol referred to by H should be considered
3069 to resolve local to the current module, and false otherwise. Differs
3070 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3071 undefined symbols. The two functions are virtually identical except
3072 for the place where dynindx == -1 is tested. If that test is true,
3073 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3074 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3076 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3077 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3078 treatment of undefined weak symbols. For those that do not make
3079 undefined weak symbols dynamic, both functions may return false. */
3082 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3083 struct bfd_link_info *info,
3084 bfd_boolean local_protected)
3086 const struct elf_backend_data *bed;
3087 struct elf_link_hash_table *hash_table;
3089 /* If it's a local sym, of course we resolve locally. */
3093 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3094 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3095 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3098 /* Forced local symbols resolve locally. */
3099 if (h->forced_local)
3102 /* Common symbols that become definitions don't get the DEF_REGULAR
3103 flag set, so test it first, and don't bail out. */
3104 if (ELF_COMMON_DEF_P (h))
3106 /* If we don't have a definition in a regular file, then we can't
3107 resolve locally. The sym is either undefined or dynamic. */
3108 else if (!h->def_regular)
3111 /* Non-dynamic symbols resolve locally. */
3112 if (h->dynindx == -1)
3115 /* At this point, we know the symbol is defined and dynamic. In an
3116 executable it must resolve locally, likewise when building symbolic
3117 shared libraries. */
3118 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3121 /* Now deal with defined dynamic symbols in shared libraries. Ones
3122 with default visibility might not resolve locally. */
3123 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3126 hash_table = elf_hash_table (info);
3127 if (!is_elf_hash_table (hash_table))
3130 bed = get_elf_backend_data (hash_table->dynobj);
3132 /* If extern_protected_data is false, STV_PROTECTED non-function
3133 symbols are local. */
3134 if ((!info->extern_protected_data
3135 || (info->extern_protected_data < 0
3136 && !bed->extern_protected_data))
3137 && !bed->is_function_type (h->type))
3140 /* Function pointer equality tests may require that STV_PROTECTED
3141 symbols be treated as dynamic symbols. If the address of a
3142 function not defined in an executable is set to that function's
3143 plt entry in the executable, then the address of the function in
3144 a shared library must also be the plt entry in the executable. */
3145 return local_protected;
3148 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3149 aligned. Returns the first TLS output section. */
3151 struct bfd_section *
3152 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3154 struct bfd_section *sec, *tls;
3155 unsigned int align = 0;
3157 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3158 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3162 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3163 if (sec->alignment_power > align)
3164 align = sec->alignment_power;
3166 elf_hash_table (info)->tls_sec = tls;
3168 /* Ensure the alignment of the first section is the largest alignment,
3169 so that the tls segment starts aligned. */
3171 tls->alignment_power = align;
3176 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3178 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3179 Elf_Internal_Sym *sym)
3181 const struct elf_backend_data *bed;
3183 /* Local symbols do not count, but target specific ones might. */
3184 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3185 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3188 bed = get_elf_backend_data (abfd);
3189 /* Function symbols do not count. */
3190 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3193 /* If the section is undefined, then so is the symbol. */
3194 if (sym->st_shndx == SHN_UNDEF)
3197 /* If the symbol is defined in the common section, then
3198 it is a common definition and so does not count. */
3199 if (bed->common_definition (sym))
3202 /* If the symbol is in a target specific section then we
3203 must rely upon the backend to tell us what it is. */
3204 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3205 /* FIXME - this function is not coded yet:
3207 return _bfd_is_global_symbol_definition (abfd, sym);
3209 Instead for now assume that the definition is not global,
3210 Even if this is wrong, at least the linker will behave
3211 in the same way that it used to do. */
3217 /* Search the symbol table of the archive element of the archive ABFD
3218 whose archive map contains a mention of SYMDEF, and determine if
3219 the symbol is defined in this element. */
3221 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3223 Elf_Internal_Shdr * hdr;
3227 Elf_Internal_Sym *isymbuf;
3228 Elf_Internal_Sym *isym;
3229 Elf_Internal_Sym *isymend;
3232 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3236 if (! bfd_check_format (abfd, bfd_object))
3239 /* Select the appropriate symbol table. If we don't know if the
3240 object file is an IR object, give linker LTO plugin a chance to
3241 get the correct symbol table. */
3242 if (abfd->plugin_format == bfd_plugin_yes
3243 #if BFD_SUPPORTS_PLUGINS
3244 || (abfd->plugin_format == bfd_plugin_unknown
3245 && bfd_link_plugin_object_p (abfd))
3249 /* Use the IR symbol table if the object has been claimed by
3251 abfd = abfd->plugin_dummy_bfd;
3252 hdr = &elf_tdata (abfd)->symtab_hdr;
3254 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3255 hdr = &elf_tdata (abfd)->symtab_hdr;
3257 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3259 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3261 /* The sh_info field of the symtab header tells us where the
3262 external symbols start. We don't care about the local symbols. */
3263 if (elf_bad_symtab (abfd))
3265 extsymcount = symcount;
3270 extsymcount = symcount - hdr->sh_info;
3271 extsymoff = hdr->sh_info;
3274 if (extsymcount == 0)
3277 /* Read in the symbol table. */
3278 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3280 if (isymbuf == NULL)
3283 /* Scan the symbol table looking for SYMDEF. */
3285 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3289 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3294 if (strcmp (name, symdef->name) == 0)
3296 result = is_global_data_symbol_definition (abfd, isym);
3306 /* Add an entry to the .dynamic table. */
3309 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3313 struct elf_link_hash_table *hash_table;
3314 const struct elf_backend_data *bed;
3316 bfd_size_type newsize;
3317 bfd_byte *newcontents;
3318 Elf_Internal_Dyn dyn;
3320 hash_table = elf_hash_table (info);
3321 if (! is_elf_hash_table (hash_table))
3324 bed = get_elf_backend_data (hash_table->dynobj);
3325 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3326 BFD_ASSERT (s != NULL);
3328 newsize = s->size + bed->s->sizeof_dyn;
3329 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3330 if (newcontents == NULL)
3334 dyn.d_un.d_val = val;
3335 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3338 s->contents = newcontents;
3343 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3344 otherwise just check whether one already exists. Returns -1 on error,
3345 1 if a DT_NEEDED tag already exists, and 0 on success. */
3348 elf_add_dt_needed_tag (bfd *abfd,
3349 struct bfd_link_info *info,
3353 struct elf_link_hash_table *hash_table;
3356 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3359 hash_table = elf_hash_table (info);
3360 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3361 if (strindex == (size_t) -1)
3364 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3367 const struct elf_backend_data *bed;
3370 bed = get_elf_backend_data (hash_table->dynobj);
3371 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3373 for (extdyn = sdyn->contents;
3374 extdyn < sdyn->contents + sdyn->size;
3375 extdyn += bed->s->sizeof_dyn)
3377 Elf_Internal_Dyn dyn;
3379 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3380 if (dyn.d_tag == DT_NEEDED
3381 && dyn.d_un.d_val == strindex)
3383 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3391 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3394 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3398 /* We were just checking for existence of the tag. */
3399 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3404 /* Return true if SONAME is on the needed list between NEEDED and STOP
3405 (or the end of list if STOP is NULL), and needed by a library that
3409 on_needed_list (const char *soname,
3410 struct bfd_link_needed_list *needed,
3411 struct bfd_link_needed_list *stop)
3413 struct bfd_link_needed_list *look;
3414 for (look = needed; look != stop; look = look->next)
3415 if (strcmp (soname, look->name) == 0
3416 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3417 /* If needed by a library that itself is not directly
3418 needed, recursively check whether that library is
3419 indirectly needed. Since we add DT_NEEDED entries to
3420 the end of the list, library dependencies appear after
3421 the library. Therefore search prior to the current
3422 LOOK, preventing possible infinite recursion. */
3423 || on_needed_list (elf_dt_name (look->by), needed, look)))
3429 /* Sort symbol by value, section, and size. */
3431 elf_sort_symbol (const void *arg1, const void *arg2)
3433 const struct elf_link_hash_entry *h1;
3434 const struct elf_link_hash_entry *h2;
3435 bfd_signed_vma vdiff;
3437 h1 = *(const struct elf_link_hash_entry **) arg1;
3438 h2 = *(const struct elf_link_hash_entry **) arg2;
3439 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3441 return vdiff > 0 ? 1 : -1;
3444 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3446 return sdiff > 0 ? 1 : -1;
3448 vdiff = h1->size - h2->size;
3449 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3452 /* This function is used to adjust offsets into .dynstr for
3453 dynamic symbols. This is called via elf_link_hash_traverse. */
3456 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3458 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3460 if (h->dynindx != -1)
3461 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3465 /* Assign string offsets in .dynstr, update all structures referencing
3469 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3471 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3472 struct elf_link_local_dynamic_entry *entry;
3473 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3474 bfd *dynobj = hash_table->dynobj;
3477 const struct elf_backend_data *bed;
3480 _bfd_elf_strtab_finalize (dynstr);
3481 size = _bfd_elf_strtab_size (dynstr);
3483 bed = get_elf_backend_data (dynobj);
3484 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3485 BFD_ASSERT (sdyn != NULL);
3487 /* Update all .dynamic entries referencing .dynstr strings. */
3488 for (extdyn = sdyn->contents;
3489 extdyn < sdyn->contents + sdyn->size;
3490 extdyn += bed->s->sizeof_dyn)
3492 Elf_Internal_Dyn dyn;
3494 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3498 dyn.d_un.d_val = size;
3508 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3513 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3516 /* Now update local dynamic symbols. */
3517 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3518 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3519 entry->isym.st_name);
3521 /* And the rest of dynamic symbols. */
3522 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3524 /* Adjust version definitions. */
3525 if (elf_tdata (output_bfd)->cverdefs)
3530 Elf_Internal_Verdef def;
3531 Elf_Internal_Verdaux defaux;
3533 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3537 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3539 p += sizeof (Elf_External_Verdef);
3540 if (def.vd_aux != sizeof (Elf_External_Verdef))
3542 for (i = 0; i < def.vd_cnt; ++i)
3544 _bfd_elf_swap_verdaux_in (output_bfd,
3545 (Elf_External_Verdaux *) p, &defaux);
3546 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3548 _bfd_elf_swap_verdaux_out (output_bfd,
3549 &defaux, (Elf_External_Verdaux *) p);
3550 p += sizeof (Elf_External_Verdaux);
3553 while (def.vd_next);
3556 /* Adjust version references. */
3557 if (elf_tdata (output_bfd)->verref)
3562 Elf_Internal_Verneed need;
3563 Elf_Internal_Vernaux needaux;
3565 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3569 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3571 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3572 _bfd_elf_swap_verneed_out (output_bfd, &need,
3573 (Elf_External_Verneed *) p);
3574 p += sizeof (Elf_External_Verneed);
3575 for (i = 0; i < need.vn_cnt; ++i)
3577 _bfd_elf_swap_vernaux_in (output_bfd,
3578 (Elf_External_Vernaux *) p, &needaux);
3579 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3581 _bfd_elf_swap_vernaux_out (output_bfd,
3583 (Elf_External_Vernaux *) p);
3584 p += sizeof (Elf_External_Vernaux);
3587 while (need.vn_next);
3593 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3594 The default is to only match when the INPUT and OUTPUT are exactly
3598 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3599 const bfd_target *output)
3601 return input == output;
3604 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3605 This version is used when different targets for the same architecture
3606 are virtually identical. */
3609 _bfd_elf_relocs_compatible (const bfd_target *input,
3610 const bfd_target *output)
3612 const struct elf_backend_data *obed, *ibed;
3614 if (input == output)
3617 ibed = xvec_get_elf_backend_data (input);
3618 obed = xvec_get_elf_backend_data (output);
3620 if (ibed->arch != obed->arch)
3623 /* If both backends are using this function, deem them compatible. */
3624 return ibed->relocs_compatible == obed->relocs_compatible;
3627 /* Make a special call to the linker "notice" function to tell it that
3628 we are about to handle an as-needed lib, or have finished
3629 processing the lib. */
3632 _bfd_elf_notice_as_needed (bfd *ibfd,
3633 struct bfd_link_info *info,
3634 enum notice_asneeded_action act)
3636 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3639 /* Check relocations an ELF object file. */
3642 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3644 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3645 struct elf_link_hash_table *htab = elf_hash_table (info);
3647 /* If this object is the same format as the output object, and it is
3648 not a shared library, then let the backend look through the
3651 This is required to build global offset table entries and to
3652 arrange for dynamic relocs. It is not required for the
3653 particular common case of linking non PIC code, even when linking
3654 against shared libraries, but unfortunately there is no way of
3655 knowing whether an object file has been compiled PIC or not.
3656 Looking through the relocs is not particularly time consuming.
3657 The problem is that we must either (1) keep the relocs in memory,
3658 which causes the linker to require additional runtime memory or
3659 (2) read the relocs twice from the input file, which wastes time.
3660 This would be a good case for using mmap.
3662 I have no idea how to handle linking PIC code into a file of a
3663 different format. It probably can't be done. */
3664 if ((abfd->flags & DYNAMIC) == 0
3665 && is_elf_hash_table (htab)
3666 && bed->check_relocs != NULL
3667 && elf_object_id (abfd) == elf_hash_table_id (htab)
3668 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3672 for (o = abfd->sections; o != NULL; o = o->next)
3674 Elf_Internal_Rela *internal_relocs;
3677 /* Don't check relocations in excluded sections. */
3678 if ((o->flags & SEC_RELOC) == 0
3679 || (o->flags & SEC_EXCLUDE) != 0
3680 || o->reloc_count == 0
3681 || ((info->strip == strip_all || info->strip == strip_debugger)
3682 && (o->flags & SEC_DEBUGGING) != 0)
3683 || bfd_is_abs_section (o->output_section))
3686 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3688 if (internal_relocs == NULL)
3691 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3693 if (elf_section_data (o)->relocs != internal_relocs)
3694 free (internal_relocs);
3704 /* Add symbols from an ELF object file to the linker hash table. */
3707 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3709 Elf_Internal_Ehdr *ehdr;
3710 Elf_Internal_Shdr *hdr;
3714 struct elf_link_hash_entry **sym_hash;
3715 bfd_boolean dynamic;
3716 Elf_External_Versym *extversym = NULL;
3717 Elf_External_Versym *ever;
3718 struct elf_link_hash_entry *weaks;
3719 struct elf_link_hash_entry **nondeflt_vers = NULL;
3720 size_t nondeflt_vers_cnt = 0;
3721 Elf_Internal_Sym *isymbuf = NULL;
3722 Elf_Internal_Sym *isym;
3723 Elf_Internal_Sym *isymend;
3724 const struct elf_backend_data *bed;
3725 bfd_boolean add_needed;
3726 struct elf_link_hash_table *htab;
3728 void *alloc_mark = NULL;
3729 struct bfd_hash_entry **old_table = NULL;
3730 unsigned int old_size = 0;
3731 unsigned int old_count = 0;
3732 void *old_tab = NULL;
3734 struct bfd_link_hash_entry *old_undefs = NULL;
3735 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3736 void *old_strtab = NULL;
3739 bfd_boolean just_syms;
3741 htab = elf_hash_table (info);
3742 bed = get_elf_backend_data (abfd);
3744 if ((abfd->flags & DYNAMIC) == 0)
3750 /* You can't use -r against a dynamic object. Also, there's no
3751 hope of using a dynamic object which does not exactly match
3752 the format of the output file. */
3753 if (bfd_link_relocatable (info)
3754 || !is_elf_hash_table (htab)
3755 || info->output_bfd->xvec != abfd->xvec)
3757 if (bfd_link_relocatable (info))
3758 bfd_set_error (bfd_error_invalid_operation);
3760 bfd_set_error (bfd_error_wrong_format);
3765 ehdr = elf_elfheader (abfd);
3766 if (info->warn_alternate_em
3767 && bed->elf_machine_code != ehdr->e_machine
3768 && ((bed->elf_machine_alt1 != 0
3769 && ehdr->e_machine == bed->elf_machine_alt1)
3770 || (bed->elf_machine_alt2 != 0
3771 && ehdr->e_machine == bed->elf_machine_alt2)))
3772 info->callbacks->einfo
3773 /* xgettext:c-format */
3774 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3775 ehdr->e_machine, abfd, bed->elf_machine_code);
3777 /* As a GNU extension, any input sections which are named
3778 .gnu.warning.SYMBOL are treated as warning symbols for the given
3779 symbol. This differs from .gnu.warning sections, which generate
3780 warnings when they are included in an output file. */
3781 /* PR 12761: Also generate this warning when building shared libraries. */
3782 for (s = abfd->sections; s != NULL; s = s->next)
3786 name = bfd_get_section_name (abfd, s);
3787 if (CONST_STRNEQ (name, ".gnu.warning."))
3792 name += sizeof ".gnu.warning." - 1;
3794 /* If this is a shared object, then look up the symbol
3795 in the hash table. If it is there, and it is already
3796 been defined, then we will not be using the entry
3797 from this shared object, so we don't need to warn.
3798 FIXME: If we see the definition in a regular object
3799 later on, we will warn, but we shouldn't. The only
3800 fix is to keep track of what warnings we are supposed
3801 to emit, and then handle them all at the end of the
3805 struct elf_link_hash_entry *h;
3807 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3809 /* FIXME: What about bfd_link_hash_common? */
3811 && (h->root.type == bfd_link_hash_defined
3812 || h->root.type == bfd_link_hash_defweak))
3817 msg = (char *) bfd_alloc (abfd, sz + 1);
3821 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3826 if (! (_bfd_generic_link_add_one_symbol
3827 (info, abfd, name, BSF_WARNING, s, 0, msg,
3828 FALSE, bed->collect, NULL)))
3831 if (bfd_link_executable (info))
3833 /* Clobber the section size so that the warning does
3834 not get copied into the output file. */
3837 /* Also set SEC_EXCLUDE, so that symbols defined in
3838 the warning section don't get copied to the output. */
3839 s->flags |= SEC_EXCLUDE;
3844 just_syms = ((s = abfd->sections) != NULL
3845 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3850 /* If we are creating a shared library, create all the dynamic
3851 sections immediately. We need to attach them to something,
3852 so we attach them to this BFD, provided it is the right
3853 format and is not from ld --just-symbols. Always create the
3854 dynamic sections for -E/--dynamic-list. FIXME: If there
3855 are no input BFD's of the same format as the output, we can't
3856 make a shared library. */
3858 && (bfd_link_pic (info)
3859 || (!bfd_link_relocatable (info)
3861 && (info->export_dynamic || info->dynamic)))
3862 && is_elf_hash_table (htab)
3863 && info->output_bfd->xvec == abfd->xvec
3864 && !htab->dynamic_sections_created)
3866 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3870 else if (!is_elf_hash_table (htab))
3874 const char *soname = NULL;
3876 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3877 const Elf_Internal_Phdr *phdr;
3880 /* ld --just-symbols and dynamic objects don't mix very well.
3881 ld shouldn't allow it. */
3885 /* If this dynamic lib was specified on the command line with
3886 --as-needed in effect, then we don't want to add a DT_NEEDED
3887 tag unless the lib is actually used. Similary for libs brought
3888 in by another lib's DT_NEEDED. When --no-add-needed is used
3889 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3890 any dynamic library in DT_NEEDED tags in the dynamic lib at
3892 add_needed = (elf_dyn_lib_class (abfd)
3893 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3894 | DYN_NO_NEEDED)) == 0;
3896 s = bfd_get_section_by_name (abfd, ".dynamic");
3901 unsigned int elfsec;
3902 unsigned long shlink;
3904 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3911 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3912 if (elfsec == SHN_BAD)
3913 goto error_free_dyn;
3914 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3916 for (extdyn = dynbuf;
3917 extdyn < dynbuf + s->size;
3918 extdyn += bed->s->sizeof_dyn)
3920 Elf_Internal_Dyn dyn;
3922 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3923 if (dyn.d_tag == DT_SONAME)
3925 unsigned int tagv = dyn.d_un.d_val;
3926 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3928 goto error_free_dyn;
3930 if (dyn.d_tag == DT_NEEDED)
3932 struct bfd_link_needed_list *n, **pn;
3934 unsigned int tagv = dyn.d_un.d_val;
3936 amt = sizeof (struct bfd_link_needed_list);
3937 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3938 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3939 if (n == NULL || fnm == NULL)
3940 goto error_free_dyn;
3941 amt = strlen (fnm) + 1;
3942 anm = (char *) bfd_alloc (abfd, amt);
3944 goto error_free_dyn;
3945 memcpy (anm, fnm, amt);
3949 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3953 if (dyn.d_tag == DT_RUNPATH)
3955 struct bfd_link_needed_list *n, **pn;
3957 unsigned int tagv = dyn.d_un.d_val;
3959 amt = sizeof (struct bfd_link_needed_list);
3960 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3961 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3962 if (n == NULL || fnm == NULL)
3963 goto error_free_dyn;
3964 amt = strlen (fnm) + 1;
3965 anm = (char *) bfd_alloc (abfd, amt);
3967 goto error_free_dyn;
3968 memcpy (anm, fnm, amt);
3972 for (pn = & runpath;
3978 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3979 if (!runpath && dyn.d_tag == DT_RPATH)
3981 struct bfd_link_needed_list *n, **pn;
3983 unsigned int tagv = dyn.d_un.d_val;
3985 amt = sizeof (struct bfd_link_needed_list);
3986 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3987 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3988 if (n == NULL || fnm == NULL)
3989 goto error_free_dyn;
3990 amt = strlen (fnm) + 1;
3991 anm = (char *) bfd_alloc (abfd, amt);
3993 goto error_free_dyn;
3994 memcpy (anm, fnm, amt);
4004 if (dyn.d_tag == DT_AUDIT)
4006 unsigned int tagv = dyn.d_un.d_val;
4007 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4014 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4015 frees all more recently bfd_alloc'd blocks as well. */
4021 struct bfd_link_needed_list **pn;
4022 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4027 /* If we have a PT_GNU_RELRO program header, mark as read-only
4028 all sections contained fully therein. This makes relro
4029 shared library sections appear as they will at run-time. */
4030 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4031 while (--phdr >= elf_tdata (abfd)->phdr)
4032 if (phdr->p_type == PT_GNU_RELRO)
4034 for (s = abfd->sections; s != NULL; s = s->next)
4035 if ((s->flags & SEC_ALLOC) != 0
4036 && s->vma >= phdr->p_vaddr
4037 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4038 s->flags |= SEC_READONLY;
4042 /* We do not want to include any of the sections in a dynamic
4043 object in the output file. We hack by simply clobbering the
4044 list of sections in the BFD. This could be handled more
4045 cleanly by, say, a new section flag; the existing
4046 SEC_NEVER_LOAD flag is not the one we want, because that one
4047 still implies that the section takes up space in the output
4049 bfd_section_list_clear (abfd);
4051 /* Find the name to use in a DT_NEEDED entry that refers to this
4052 object. If the object has a DT_SONAME entry, we use it.
4053 Otherwise, if the generic linker stuck something in
4054 elf_dt_name, we use that. Otherwise, we just use the file
4056 if (soname == NULL || *soname == '\0')
4058 soname = elf_dt_name (abfd);
4059 if (soname == NULL || *soname == '\0')
4060 soname = bfd_get_filename (abfd);
4063 /* Save the SONAME because sometimes the linker emulation code
4064 will need to know it. */
4065 elf_dt_name (abfd) = soname;
4067 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4071 /* If we have already included this dynamic object in the
4072 link, just ignore it. There is no reason to include a
4073 particular dynamic object more than once. */
4077 /* Save the DT_AUDIT entry for the linker emulation code. */
4078 elf_dt_audit (abfd) = audit;
4081 /* If this is a dynamic object, we always link against the .dynsym
4082 symbol table, not the .symtab symbol table. The dynamic linker
4083 will only see the .dynsym symbol table, so there is no reason to
4084 look at .symtab for a dynamic object. */
4086 if (! dynamic || elf_dynsymtab (abfd) == 0)
4087 hdr = &elf_tdata (abfd)->symtab_hdr;
4089 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4091 symcount = hdr->sh_size / bed->s->sizeof_sym;
4093 /* The sh_info field of the symtab header tells us where the
4094 external symbols start. We don't care about the local symbols at
4096 if (elf_bad_symtab (abfd))
4098 extsymcount = symcount;
4103 extsymcount = symcount - hdr->sh_info;
4104 extsymoff = hdr->sh_info;
4107 sym_hash = elf_sym_hashes (abfd);
4108 if (extsymcount != 0)
4110 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4112 if (isymbuf == NULL)
4115 if (sym_hash == NULL)
4117 /* We store a pointer to the hash table entry for each
4120 amt *= sizeof (struct elf_link_hash_entry *);
4121 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4122 if (sym_hash == NULL)
4123 goto error_free_sym;
4124 elf_sym_hashes (abfd) = sym_hash;
4130 /* Read in any version definitions. */
4131 if (!_bfd_elf_slurp_version_tables (abfd,
4132 info->default_imported_symver))
4133 goto error_free_sym;
4135 /* Read in the symbol versions, but don't bother to convert them
4136 to internal format. */
4137 if (elf_dynversym (abfd) != 0)
4139 Elf_Internal_Shdr *versymhdr;
4141 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4142 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4143 if (extversym == NULL)
4144 goto error_free_sym;
4145 amt = versymhdr->sh_size;
4146 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4147 || bfd_bread (extversym, amt, abfd) != amt)
4148 goto error_free_vers;
4152 /* If we are loading an as-needed shared lib, save the symbol table
4153 state before we start adding symbols. If the lib turns out
4154 to be unneeded, restore the state. */
4155 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4160 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4162 struct bfd_hash_entry *p;
4163 struct elf_link_hash_entry *h;
4165 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4167 h = (struct elf_link_hash_entry *) p;
4168 entsize += htab->root.table.entsize;
4169 if (h->root.type == bfd_link_hash_warning)
4170 entsize += htab->root.table.entsize;
4174 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4175 old_tab = bfd_malloc (tabsize + entsize);
4176 if (old_tab == NULL)
4177 goto error_free_vers;
4179 /* Remember the current objalloc pointer, so that all mem for
4180 symbols added can later be reclaimed. */
4181 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4182 if (alloc_mark == NULL)
4183 goto error_free_vers;
4185 /* Make a special call to the linker "notice" function to
4186 tell it that we are about to handle an as-needed lib. */
4187 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4188 goto error_free_vers;
4190 /* Clone the symbol table. Remember some pointers into the
4191 symbol table, and dynamic symbol count. */
4192 old_ent = (char *) old_tab + tabsize;
4193 memcpy (old_tab, htab->root.table.table, tabsize);
4194 old_undefs = htab->root.undefs;
4195 old_undefs_tail = htab->root.undefs_tail;
4196 old_table = htab->root.table.table;
4197 old_size = htab->root.table.size;
4198 old_count = htab->root.table.count;
4199 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4200 if (old_strtab == NULL)
4201 goto error_free_vers;
4203 for (i = 0; i < htab->root.table.size; i++)
4205 struct bfd_hash_entry *p;
4206 struct elf_link_hash_entry *h;
4208 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4210 memcpy (old_ent, p, htab->root.table.entsize);
4211 old_ent = (char *) old_ent + htab->root.table.entsize;
4212 h = (struct elf_link_hash_entry *) p;
4213 if (h->root.type == bfd_link_hash_warning)
4215 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4216 old_ent = (char *) old_ent + htab->root.table.entsize;
4223 ever = extversym != NULL ? extversym + extsymoff : NULL;
4224 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4226 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4230 asection *sec, *new_sec;
4233 struct elf_link_hash_entry *h;
4234 struct elf_link_hash_entry *hi;
4235 bfd_boolean definition;
4236 bfd_boolean size_change_ok;
4237 bfd_boolean type_change_ok;
4238 bfd_boolean new_weakdef;
4239 bfd_boolean new_weak;
4240 bfd_boolean old_weak;
4241 bfd_boolean override;
4243 bfd_boolean discarded;
4244 unsigned int old_alignment;
4246 bfd_boolean matched;
4250 flags = BSF_NO_FLAGS;
4252 value = isym->st_value;
4253 common = bed->common_definition (isym);
4256 bind = ELF_ST_BIND (isym->st_info);
4260 /* This should be impossible, since ELF requires that all
4261 global symbols follow all local symbols, and that sh_info
4262 point to the first global symbol. Unfortunately, Irix 5
4267 if (isym->st_shndx != SHN_UNDEF && !common)
4275 case STB_GNU_UNIQUE:
4276 flags = BSF_GNU_UNIQUE;
4280 /* Leave it up to the processor backend. */
4284 if (isym->st_shndx == SHN_UNDEF)
4285 sec = bfd_und_section_ptr;
4286 else if (isym->st_shndx == SHN_ABS)
4287 sec = bfd_abs_section_ptr;
4288 else if (isym->st_shndx == SHN_COMMON)
4290 sec = bfd_com_section_ptr;
4291 /* What ELF calls the size we call the value. What ELF
4292 calls the value we call the alignment. */
4293 value = isym->st_size;
4297 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4299 sec = bfd_abs_section_ptr;
4300 else if (discarded_section (sec))
4302 /* Symbols from discarded section are undefined. We keep
4304 sec = bfd_und_section_ptr;
4306 isym->st_shndx = SHN_UNDEF;
4308 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4312 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4315 goto error_free_vers;
4317 if (isym->st_shndx == SHN_COMMON
4318 && (abfd->flags & BFD_PLUGIN) != 0)
4320 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4324 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4326 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4328 goto error_free_vers;
4332 else if (isym->st_shndx == SHN_COMMON
4333 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4334 && !bfd_link_relocatable (info))
4336 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4340 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4341 | SEC_LINKER_CREATED);
4342 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4344 goto error_free_vers;
4348 else if (bed->elf_add_symbol_hook)
4350 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4352 goto error_free_vers;
4354 /* The hook function sets the name to NULL if this symbol
4355 should be skipped for some reason. */
4360 /* Sanity check that all possibilities were handled. */
4363 bfd_set_error (bfd_error_bad_value);
4364 goto error_free_vers;
4367 /* Silently discard TLS symbols from --just-syms. There's
4368 no way to combine a static TLS block with a new TLS block
4369 for this executable. */
4370 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4371 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4374 if (bfd_is_und_section (sec)
4375 || bfd_is_com_section (sec))
4380 size_change_ok = FALSE;
4381 type_change_ok = bed->type_change_ok;
4388 if (is_elf_hash_table (htab))
4390 Elf_Internal_Versym iver;
4391 unsigned int vernum = 0;
4396 if (info->default_imported_symver)
4397 /* Use the default symbol version created earlier. */
4398 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4403 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4405 vernum = iver.vs_vers & VERSYM_VERSION;
4407 /* If this is a hidden symbol, or if it is not version
4408 1, we append the version name to the symbol name.
4409 However, we do not modify a non-hidden absolute symbol
4410 if it is not a function, because it might be the version
4411 symbol itself. FIXME: What if it isn't? */
4412 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4414 && (!bfd_is_abs_section (sec)
4415 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4418 size_t namelen, verlen, newlen;
4421 if (isym->st_shndx != SHN_UNDEF)
4423 if (vernum > elf_tdata (abfd)->cverdefs)
4425 else if (vernum > 1)
4427 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4434 /* xgettext:c-format */
4435 (_("%B: %s: invalid version %u (max %d)"),
4437 elf_tdata (abfd)->cverdefs);
4438 bfd_set_error (bfd_error_bad_value);
4439 goto error_free_vers;
4444 /* We cannot simply test for the number of
4445 entries in the VERNEED section since the
4446 numbers for the needed versions do not start
4448 Elf_Internal_Verneed *t;
4451 for (t = elf_tdata (abfd)->verref;
4455 Elf_Internal_Vernaux *a;
4457 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4459 if (a->vna_other == vernum)
4461 verstr = a->vna_nodename;
4471 /* xgettext:c-format */
4472 (_("%B: %s: invalid needed version %d"),
4473 abfd, name, vernum);
4474 bfd_set_error (bfd_error_bad_value);
4475 goto error_free_vers;
4479 namelen = strlen (name);
4480 verlen = strlen (verstr);
4481 newlen = namelen + verlen + 2;
4482 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4483 && isym->st_shndx != SHN_UNDEF)
4486 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4487 if (newname == NULL)
4488 goto error_free_vers;
4489 memcpy (newname, name, namelen);
4490 p = newname + namelen;
4492 /* If this is a defined non-hidden version symbol,
4493 we add another @ to the name. This indicates the
4494 default version of the symbol. */
4495 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4496 && isym->st_shndx != SHN_UNDEF)
4498 memcpy (p, verstr, verlen + 1);
4503 /* If this symbol has default visibility and the user has
4504 requested we not re-export it, then mark it as hidden. */
4505 if (!bfd_is_und_section (sec)
4508 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4509 isym->st_other = (STV_HIDDEN
4510 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4512 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4513 sym_hash, &old_bfd, &old_weak,
4514 &old_alignment, &skip, &override,
4515 &type_change_ok, &size_change_ok,
4517 goto error_free_vers;
4522 /* Override a definition only if the new symbol matches the
4524 if (override && matched)
4528 while (h->root.type == bfd_link_hash_indirect
4529 || h->root.type == bfd_link_hash_warning)
4530 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4532 if (elf_tdata (abfd)->verdef != NULL
4535 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4538 if (! (_bfd_generic_link_add_one_symbol
4539 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4540 (struct bfd_link_hash_entry **) sym_hash)))
4541 goto error_free_vers;
4543 if ((flags & BSF_GNU_UNIQUE)
4544 && (abfd->flags & DYNAMIC) == 0
4545 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4546 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4549 /* We need to make sure that indirect symbol dynamic flags are
4552 while (h->root.type == bfd_link_hash_indirect
4553 || h->root.type == bfd_link_hash_warning)
4554 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4556 /* Setting the index to -3 tells elf_link_output_extsym that
4557 this symbol is defined in a discarded section. */
4563 new_weak = (flags & BSF_WEAK) != 0;
4564 new_weakdef = FALSE;
4568 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4569 && is_elf_hash_table (htab)
4570 && h->u.weakdef == NULL)
4572 /* Keep a list of all weak defined non function symbols from
4573 a dynamic object, using the weakdef field. Later in this
4574 function we will set the weakdef field to the correct
4575 value. We only put non-function symbols from dynamic
4576 objects on this list, because that happens to be the only
4577 time we need to know the normal symbol corresponding to a
4578 weak symbol, and the information is time consuming to
4579 figure out. If the weakdef field is not already NULL,
4580 then this symbol was already defined by some previous
4581 dynamic object, and we will be using that previous
4582 definition anyhow. */
4584 h->u.weakdef = weaks;
4589 /* Set the alignment of a common symbol. */
4590 if ((common || bfd_is_com_section (sec))
4591 && h->root.type == bfd_link_hash_common)
4596 align = bfd_log2 (isym->st_value);
4599 /* The new symbol is a common symbol in a shared object.
4600 We need to get the alignment from the section. */
4601 align = new_sec->alignment_power;
4603 if (align > old_alignment)
4604 h->root.u.c.p->alignment_power = align;
4606 h->root.u.c.p->alignment_power = old_alignment;
4609 if (is_elf_hash_table (htab))
4611 /* Set a flag in the hash table entry indicating the type of
4612 reference or definition we just found. A dynamic symbol
4613 is one which is referenced or defined by both a regular
4614 object and a shared object. */
4615 bfd_boolean dynsym = FALSE;
4617 /* Plugin symbols aren't normal. Don't set def_regular or
4618 ref_regular for them, or make them dynamic. */
4619 if ((abfd->flags & BFD_PLUGIN) != 0)
4626 if (bind != STB_WEAK)
4627 h->ref_regular_nonweak = 1;
4639 /* If the indirect symbol has been forced local, don't
4640 make the real symbol dynamic. */
4641 if ((h == hi || !hi->forced_local)
4642 && (bfd_link_dll (info)
4652 hi->ref_dynamic = 1;
4657 hi->def_dynamic = 1;
4660 /* If the indirect symbol has been forced local, don't
4661 make the real symbol dynamic. */
4662 if ((h == hi || !hi->forced_local)
4665 || (h->u.weakdef != NULL
4667 && h->u.weakdef->dynindx != -1)))
4671 /* Check to see if we need to add an indirect symbol for
4672 the default name. */
4674 || (!override && h->root.type == bfd_link_hash_common))
4675 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4676 sec, value, &old_bfd, &dynsym))
4677 goto error_free_vers;
4679 /* Check the alignment when a common symbol is involved. This
4680 can change when a common symbol is overridden by a normal
4681 definition or a common symbol is ignored due to the old
4682 normal definition. We need to make sure the maximum
4683 alignment is maintained. */
4684 if ((old_alignment || common)
4685 && h->root.type != bfd_link_hash_common)
4687 unsigned int common_align;
4688 unsigned int normal_align;
4689 unsigned int symbol_align;
4693 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4694 || h->root.type == bfd_link_hash_defweak);
4696 symbol_align = ffs (h->root.u.def.value) - 1;
4697 if (h->root.u.def.section->owner != NULL
4698 && (h->root.u.def.section->owner->flags
4699 & (DYNAMIC | BFD_PLUGIN)) == 0)
4701 normal_align = h->root.u.def.section->alignment_power;
4702 if (normal_align > symbol_align)
4703 normal_align = symbol_align;
4706 normal_align = symbol_align;
4710 common_align = old_alignment;
4711 common_bfd = old_bfd;
4716 common_align = bfd_log2 (isym->st_value);
4718 normal_bfd = old_bfd;
4721 if (normal_align < common_align)
4723 /* PR binutils/2735 */
4724 if (normal_bfd == NULL)
4726 /* xgettext:c-format */
4727 (_("Warning: alignment %u of common symbol `%s' in %B is"
4728 " greater than the alignment (%u) of its section %A"),
4729 1 << common_align, name, common_bfd,
4730 1 << normal_align, h->root.u.def.section);
4733 /* xgettext:c-format */
4734 (_("Warning: alignment %u of symbol `%s' in %B"
4735 " is smaller than %u in %B"),
4736 1 << normal_align, name, normal_bfd,
4737 1 << common_align, common_bfd);
4741 /* Remember the symbol size if it isn't undefined. */
4742 if (isym->st_size != 0
4743 && isym->st_shndx != SHN_UNDEF
4744 && (definition || h->size == 0))
4747 && h->size != isym->st_size
4748 && ! size_change_ok)
4750 /* xgettext:c-format */
4751 (_("Warning: size of symbol `%s' changed"
4752 " from %lu in %B to %lu in %B"),
4753 name, (unsigned long) h->size, old_bfd,
4754 (unsigned long) isym->st_size, abfd);
4756 h->size = isym->st_size;
4759 /* If this is a common symbol, then we always want H->SIZE
4760 to be the size of the common symbol. The code just above
4761 won't fix the size if a common symbol becomes larger. We
4762 don't warn about a size change here, because that is
4763 covered by --warn-common. Allow changes between different
4765 if (h->root.type == bfd_link_hash_common)
4766 h->size = h->root.u.c.size;
4768 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4769 && ((definition && !new_weak)
4770 || (old_weak && h->root.type == bfd_link_hash_common)
4771 || h->type == STT_NOTYPE))
4773 unsigned int type = ELF_ST_TYPE (isym->st_info);
4775 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4777 if (type == STT_GNU_IFUNC
4778 && (abfd->flags & DYNAMIC) != 0)
4781 if (h->type != type)
4783 if (h->type != STT_NOTYPE && ! type_change_ok)
4784 /* xgettext:c-format */
4786 (_("Warning: type of symbol `%s' changed"
4787 " from %d to %d in %B"),
4788 name, h->type, type, abfd);
4794 /* Merge st_other field. */
4795 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4797 /* We don't want to make debug symbol dynamic. */
4799 && (sec->flags & SEC_DEBUGGING)
4800 && !bfd_link_relocatable (info))
4803 /* Nor should we make plugin symbols dynamic. */
4804 if ((abfd->flags & BFD_PLUGIN) != 0)
4809 h->target_internal = isym->st_target_internal;
4810 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4813 if (definition && !dynamic)
4815 char *p = strchr (name, ELF_VER_CHR);
4816 if (p != NULL && p[1] != ELF_VER_CHR)
4818 /* Queue non-default versions so that .symver x, x@FOO
4819 aliases can be checked. */
4822 amt = ((isymend - isym + 1)
4823 * sizeof (struct elf_link_hash_entry *));
4825 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4827 goto error_free_vers;
4829 nondeflt_vers[nondeflt_vers_cnt++] = h;
4833 if (dynsym && h->dynindx == -1)
4835 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4836 goto error_free_vers;
4837 if (h->u.weakdef != NULL
4839 && h->u.weakdef->dynindx == -1)
4841 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4842 goto error_free_vers;
4845 else if (h->dynindx != -1)
4846 /* If the symbol already has a dynamic index, but
4847 visibility says it should not be visible, turn it into
4849 switch (ELF_ST_VISIBILITY (h->other))
4853 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4858 /* Don't add DT_NEEDED for references from the dummy bfd nor
4859 for unmatched symbol. */
4864 && h->ref_regular_nonweak
4866 || (old_bfd->flags & BFD_PLUGIN) == 0))
4867 || (h->ref_dynamic_nonweak
4868 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4869 && !on_needed_list (elf_dt_name (abfd),
4870 htab->needed, NULL))))
4873 const char *soname = elf_dt_name (abfd);
4875 info->callbacks->minfo ("%!", soname, old_bfd,
4876 h->root.root.string);
4878 /* A symbol from a library loaded via DT_NEEDED of some
4879 other library is referenced by a regular object.
4880 Add a DT_NEEDED entry for it. Issue an error if
4881 --no-add-needed is used and the reference was not
4884 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4887 /* xgettext:c-format */
4888 (_("%B: undefined reference to symbol '%s'"),
4890 bfd_set_error (bfd_error_missing_dso);
4891 goto error_free_vers;
4894 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4895 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4898 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4900 goto error_free_vers;
4902 BFD_ASSERT (ret == 0);
4907 if (extversym != NULL)
4913 if (isymbuf != NULL)
4919 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4923 /* Restore the symbol table. */
4924 old_ent = (char *) old_tab + tabsize;
4925 memset (elf_sym_hashes (abfd), 0,
4926 extsymcount * sizeof (struct elf_link_hash_entry *));
4927 htab->root.table.table = old_table;
4928 htab->root.table.size = old_size;
4929 htab->root.table.count = old_count;
4930 memcpy (htab->root.table.table, old_tab, tabsize);
4931 htab->root.undefs = old_undefs;
4932 htab->root.undefs_tail = old_undefs_tail;
4933 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4936 for (i = 0; i < htab->root.table.size; i++)
4938 struct bfd_hash_entry *p;
4939 struct elf_link_hash_entry *h;
4941 unsigned int alignment_power;
4942 unsigned int dynamic_ref_after_ir_def;
4944 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4946 h = (struct elf_link_hash_entry *) p;
4947 if (h->root.type == bfd_link_hash_warning)
4948 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4950 /* Preserve the maximum alignment and size for common
4951 symbols even if this dynamic lib isn't on DT_NEEDED
4952 since it can still be loaded at run time by another
4954 if (h->root.type == bfd_link_hash_common)
4956 size = h->root.u.c.size;
4957 alignment_power = h->root.u.c.p->alignment_power;
4962 alignment_power = 0;
4964 /* Preserve dynamic_ref_after_ir_def so that this symbol
4965 will be exported when the dynamic lib becomes needed
4966 in the second pass. */
4967 dynamic_ref_after_ir_def = h->root.dynamic_ref_after_ir_def;
4968 memcpy (p, old_ent, htab->root.table.entsize);
4969 old_ent = (char *) old_ent + htab->root.table.entsize;
4970 h = (struct elf_link_hash_entry *) p;
4971 if (h->root.type == bfd_link_hash_warning)
4973 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4974 old_ent = (char *) old_ent + htab->root.table.entsize;
4975 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4977 if (h->root.type == bfd_link_hash_common)
4979 if (size > h->root.u.c.size)
4980 h->root.u.c.size = size;
4981 if (alignment_power > h->root.u.c.p->alignment_power)
4982 h->root.u.c.p->alignment_power = alignment_power;
4984 h->root.dynamic_ref_after_ir_def = dynamic_ref_after_ir_def;
4988 /* Make a special call to the linker "notice" function to
4989 tell it that symbols added for crefs may need to be removed. */
4990 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4991 goto error_free_vers;
4994 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4996 if (nondeflt_vers != NULL)
4997 free (nondeflt_vers);
5001 if (old_tab != NULL)
5003 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5004 goto error_free_vers;
5009 /* Now that all the symbols from this input file are created, if
5010 not performing a relocatable link, handle .symver foo, foo@BAR
5011 such that any relocs against foo become foo@BAR. */
5012 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5016 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5018 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5019 char *shortname, *p;
5021 p = strchr (h->root.root.string, ELF_VER_CHR);
5023 || (h->root.type != bfd_link_hash_defined
5024 && h->root.type != bfd_link_hash_defweak))
5027 amt = p - h->root.root.string;
5028 shortname = (char *) bfd_malloc (amt + 1);
5030 goto error_free_vers;
5031 memcpy (shortname, h->root.root.string, amt);
5032 shortname[amt] = '\0';
5034 hi = (struct elf_link_hash_entry *)
5035 bfd_link_hash_lookup (&htab->root, shortname,
5036 FALSE, FALSE, FALSE);
5038 && hi->root.type == h->root.type
5039 && hi->root.u.def.value == h->root.u.def.value
5040 && hi->root.u.def.section == h->root.u.def.section)
5042 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5043 hi->root.type = bfd_link_hash_indirect;
5044 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5045 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5046 sym_hash = elf_sym_hashes (abfd);
5048 for (symidx = 0; symidx < extsymcount; ++symidx)
5049 if (sym_hash[symidx] == hi)
5051 sym_hash[symidx] = h;
5057 free (nondeflt_vers);
5058 nondeflt_vers = NULL;
5061 /* Now set the weakdefs field correctly for all the weak defined
5062 symbols we found. The only way to do this is to search all the
5063 symbols. Since we only need the information for non functions in
5064 dynamic objects, that's the only time we actually put anything on
5065 the list WEAKS. We need this information so that if a regular
5066 object refers to a symbol defined weakly in a dynamic object, the
5067 real symbol in the dynamic object is also put in the dynamic
5068 symbols; we also must arrange for both symbols to point to the
5069 same memory location. We could handle the general case of symbol
5070 aliasing, but a general symbol alias can only be generated in
5071 assembler code, handling it correctly would be very time
5072 consuming, and other ELF linkers don't handle general aliasing
5076 struct elf_link_hash_entry **hpp;
5077 struct elf_link_hash_entry **hppend;
5078 struct elf_link_hash_entry **sorted_sym_hash;
5079 struct elf_link_hash_entry *h;
5082 /* Since we have to search the whole symbol list for each weak
5083 defined symbol, search time for N weak defined symbols will be
5084 O(N^2). Binary search will cut it down to O(NlogN). */
5086 amt *= sizeof (struct elf_link_hash_entry *);
5087 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5088 if (sorted_sym_hash == NULL)
5090 sym_hash = sorted_sym_hash;
5091 hpp = elf_sym_hashes (abfd);
5092 hppend = hpp + extsymcount;
5094 for (; hpp < hppend; hpp++)
5098 && h->root.type == bfd_link_hash_defined
5099 && !bed->is_function_type (h->type))
5107 qsort (sorted_sym_hash, sym_count,
5108 sizeof (struct elf_link_hash_entry *),
5111 while (weaks != NULL)
5113 struct elf_link_hash_entry *hlook;
5116 size_t i, j, idx = 0;
5119 weaks = hlook->u.weakdef;
5120 hlook->u.weakdef = NULL;
5122 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5123 || hlook->root.type == bfd_link_hash_defweak
5124 || hlook->root.type == bfd_link_hash_common
5125 || hlook->root.type == bfd_link_hash_indirect);
5126 slook = hlook->root.u.def.section;
5127 vlook = hlook->root.u.def.value;
5133 bfd_signed_vma vdiff;
5135 h = sorted_sym_hash[idx];
5136 vdiff = vlook - h->root.u.def.value;
5143 int sdiff = slook->id - h->root.u.def.section->id;
5153 /* We didn't find a value/section match. */
5157 /* With multiple aliases, or when the weak symbol is already
5158 strongly defined, we have multiple matching symbols and
5159 the binary search above may land on any of them. Step
5160 one past the matching symbol(s). */
5163 h = sorted_sym_hash[idx];
5164 if (h->root.u.def.section != slook
5165 || h->root.u.def.value != vlook)
5169 /* Now look back over the aliases. Since we sorted by size
5170 as well as value and section, we'll choose the one with
5171 the largest size. */
5174 h = sorted_sym_hash[idx];
5176 /* Stop if value or section doesn't match. */
5177 if (h->root.u.def.section != slook
5178 || h->root.u.def.value != vlook)
5180 else if (h != hlook)
5182 hlook->u.weakdef = h;
5184 /* If the weak definition is in the list of dynamic
5185 symbols, make sure the real definition is put
5187 if (hlook->dynindx != -1 && h->dynindx == -1)
5189 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5192 free (sorted_sym_hash);
5197 /* If the real definition is in the list of dynamic
5198 symbols, make sure the weak definition is put
5199 there as well. If we don't do this, then the
5200 dynamic loader might not merge the entries for the
5201 real definition and the weak definition. */
5202 if (h->dynindx != -1 && hlook->dynindx == -1)
5204 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5205 goto err_free_sym_hash;
5212 free (sorted_sym_hash);
5215 if (bed->check_directives
5216 && !(*bed->check_directives) (abfd, info))
5219 if (!info->check_relocs_after_open_input
5220 && !_bfd_elf_link_check_relocs (abfd, info))
5223 /* If this is a non-traditional link, try to optimize the handling
5224 of the .stab/.stabstr sections. */
5226 && ! info->traditional_format
5227 && is_elf_hash_table (htab)
5228 && (info->strip != strip_all && info->strip != strip_debugger))
5232 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5233 if (stabstr != NULL)
5235 bfd_size_type string_offset = 0;
5238 for (stab = abfd->sections; stab; stab = stab->next)
5239 if (CONST_STRNEQ (stab->name, ".stab")
5240 && (!stab->name[5] ||
5241 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5242 && (stab->flags & SEC_MERGE) == 0
5243 && !bfd_is_abs_section (stab->output_section))
5245 struct bfd_elf_section_data *secdata;
5247 secdata = elf_section_data (stab);
5248 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5249 stabstr, &secdata->sec_info,
5252 if (secdata->sec_info)
5253 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5258 if (is_elf_hash_table (htab) && add_needed)
5260 /* Add this bfd to the loaded list. */
5261 struct elf_link_loaded_list *n;
5263 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5267 n->next = htab->loaded;
5274 if (old_tab != NULL)
5276 if (old_strtab != NULL)
5278 if (nondeflt_vers != NULL)
5279 free (nondeflt_vers);
5280 if (extversym != NULL)
5283 if (isymbuf != NULL)
5289 /* Return the linker hash table entry of a symbol that might be
5290 satisfied by an archive symbol. Return -1 on error. */
5292 struct elf_link_hash_entry *
5293 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5294 struct bfd_link_info *info,
5297 struct elf_link_hash_entry *h;
5301 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5305 /* If this is a default version (the name contains @@), look up the
5306 symbol again with only one `@' as well as without the version.
5307 The effect is that references to the symbol with and without the
5308 version will be matched by the default symbol in the archive. */
5310 p = strchr (name, ELF_VER_CHR);
5311 if (p == NULL || p[1] != ELF_VER_CHR)
5314 /* First check with only one `@'. */
5315 len = strlen (name);
5316 copy = (char *) bfd_alloc (abfd, len);
5318 return (struct elf_link_hash_entry *) 0 - 1;
5320 first = p - name + 1;
5321 memcpy (copy, name, first);
5322 memcpy (copy + first, name + first + 1, len - first);
5324 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5327 /* We also need to check references to the symbol without the
5329 copy[first - 1] = '\0';
5330 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5331 FALSE, FALSE, TRUE);
5334 bfd_release (abfd, copy);
5338 /* Add symbols from an ELF archive file to the linker hash table. We
5339 don't use _bfd_generic_link_add_archive_symbols because we need to
5340 handle versioned symbols.
5342 Fortunately, ELF archive handling is simpler than that done by
5343 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5344 oddities. In ELF, if we find a symbol in the archive map, and the
5345 symbol is currently undefined, we know that we must pull in that
5348 Unfortunately, we do have to make multiple passes over the symbol
5349 table until nothing further is resolved. */
5352 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5355 unsigned char *included = NULL;
5359 const struct elf_backend_data *bed;
5360 struct elf_link_hash_entry * (*archive_symbol_lookup)
5361 (bfd *, struct bfd_link_info *, const char *);
5363 if (! bfd_has_map (abfd))
5365 /* An empty archive is a special case. */
5366 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5368 bfd_set_error (bfd_error_no_armap);
5372 /* Keep track of all symbols we know to be already defined, and all
5373 files we know to be already included. This is to speed up the
5374 second and subsequent passes. */
5375 c = bfd_ardata (abfd)->symdef_count;
5379 amt *= sizeof (*included);
5380 included = (unsigned char *) bfd_zmalloc (amt);
5381 if (included == NULL)
5384 symdefs = bfd_ardata (abfd)->symdefs;
5385 bed = get_elf_backend_data (abfd);
5386 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5399 symdefend = symdef + c;
5400 for (i = 0; symdef < symdefend; symdef++, i++)
5402 struct elf_link_hash_entry *h;
5404 struct bfd_link_hash_entry *undefs_tail;
5409 if (symdef->file_offset == last)
5415 h = archive_symbol_lookup (abfd, info, symdef->name);
5416 if (h == (struct elf_link_hash_entry *) 0 - 1)
5422 if (h->root.type == bfd_link_hash_common)
5424 /* We currently have a common symbol. The archive map contains
5425 a reference to this symbol, so we may want to include it. We
5426 only want to include it however, if this archive element
5427 contains a definition of the symbol, not just another common
5430 Unfortunately some archivers (including GNU ar) will put
5431 declarations of common symbols into their archive maps, as
5432 well as real definitions, so we cannot just go by the archive
5433 map alone. Instead we must read in the element's symbol
5434 table and check that to see what kind of symbol definition
5436 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5439 else if (h->root.type != bfd_link_hash_undefined)
5441 if (h->root.type != bfd_link_hash_undefweak)
5442 /* Symbol must be defined. Don't check it again. */
5447 /* We need to include this archive member. */
5448 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5449 if (element == NULL)
5452 if (! bfd_check_format (element, bfd_object))
5455 undefs_tail = info->hash->undefs_tail;
5457 if (!(*info->callbacks
5458 ->add_archive_element) (info, element, symdef->name, &element))
5460 if (!bfd_link_add_symbols (element, info))
5463 /* If there are any new undefined symbols, we need to make
5464 another pass through the archive in order to see whether
5465 they can be defined. FIXME: This isn't perfect, because
5466 common symbols wind up on undefs_tail and because an
5467 undefined symbol which is defined later on in this pass
5468 does not require another pass. This isn't a bug, but it
5469 does make the code less efficient than it could be. */
5470 if (undefs_tail != info->hash->undefs_tail)
5473 /* Look backward to mark all symbols from this object file
5474 which we have already seen in this pass. */
5478 included[mark] = TRUE;
5483 while (symdefs[mark].file_offset == symdef->file_offset);
5485 /* We mark subsequent symbols from this object file as we go
5486 on through the loop. */
5487 last = symdef->file_offset;
5497 if (included != NULL)
5502 /* Given an ELF BFD, add symbols to the global hash table as
5506 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5508 switch (bfd_get_format (abfd))
5511 return elf_link_add_object_symbols (abfd, info);
5513 return elf_link_add_archive_symbols (abfd, info);
5515 bfd_set_error (bfd_error_wrong_format);
5520 struct hash_codes_info
5522 unsigned long *hashcodes;
5526 /* This function will be called though elf_link_hash_traverse to store
5527 all hash value of the exported symbols in an array. */
5530 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5532 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5537 /* Ignore indirect symbols. These are added by the versioning code. */
5538 if (h->dynindx == -1)
5541 name = h->root.root.string;
5542 if (h->versioned >= versioned)
5544 char *p = strchr (name, ELF_VER_CHR);
5547 alc = (char *) bfd_malloc (p - name + 1);
5553 memcpy (alc, name, p - name);
5554 alc[p - name] = '\0';
5559 /* Compute the hash value. */
5560 ha = bfd_elf_hash (name);
5562 /* Store the found hash value in the array given as the argument. */
5563 *(inf->hashcodes)++ = ha;
5565 /* And store it in the struct so that we can put it in the hash table
5567 h->u.elf_hash_value = ha;
5575 struct collect_gnu_hash_codes
5578 const struct elf_backend_data *bed;
5579 unsigned long int nsyms;
5580 unsigned long int maskbits;
5581 unsigned long int *hashcodes;
5582 unsigned long int *hashval;
5583 unsigned long int *indx;
5584 unsigned long int *counts;
5587 long int min_dynindx;
5588 unsigned long int bucketcount;
5589 unsigned long int symindx;
5590 long int local_indx;
5591 long int shift1, shift2;
5592 unsigned long int mask;
5596 /* This function will be called though elf_link_hash_traverse to store
5597 all hash value of the exported symbols in an array. */
5600 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5602 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5607 /* Ignore indirect symbols. These are added by the versioning code. */
5608 if (h->dynindx == -1)
5611 /* Ignore also local symbols and undefined symbols. */
5612 if (! (*s->bed->elf_hash_symbol) (h))
5615 name = h->root.root.string;
5616 if (h->versioned >= versioned)
5618 char *p = strchr (name, ELF_VER_CHR);
5621 alc = (char *) bfd_malloc (p - name + 1);
5627 memcpy (alc, name, p - name);
5628 alc[p - name] = '\0';
5633 /* Compute the hash value. */
5634 ha = bfd_elf_gnu_hash (name);
5636 /* Store the found hash value in the array for compute_bucket_count,
5637 and also for .dynsym reordering purposes. */
5638 s->hashcodes[s->nsyms] = ha;
5639 s->hashval[h->dynindx] = ha;
5641 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5642 s->min_dynindx = h->dynindx;
5650 /* This function will be called though elf_link_hash_traverse to do
5651 final dynaminc symbol renumbering. */
5654 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5656 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5657 unsigned long int bucket;
5658 unsigned long int val;
5660 /* Ignore indirect symbols. */
5661 if (h->dynindx == -1)
5664 /* Ignore also local symbols and undefined symbols. */
5665 if (! (*s->bed->elf_hash_symbol) (h))
5667 if (h->dynindx >= s->min_dynindx)
5668 h->dynindx = s->local_indx++;
5672 bucket = s->hashval[h->dynindx] % s->bucketcount;
5673 val = (s->hashval[h->dynindx] >> s->shift1)
5674 & ((s->maskbits >> s->shift1) - 1);
5675 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5677 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5678 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5679 if (s->counts[bucket] == 1)
5680 /* Last element terminates the chain. */
5682 bfd_put_32 (s->output_bfd, val,
5683 s->contents + (s->indx[bucket] - s->symindx) * 4);
5684 --s->counts[bucket];
5685 h->dynindx = s->indx[bucket]++;
5689 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5692 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5694 return !(h->forced_local
5695 || h->root.type == bfd_link_hash_undefined
5696 || h->root.type == bfd_link_hash_undefweak
5697 || ((h->root.type == bfd_link_hash_defined
5698 || h->root.type == bfd_link_hash_defweak)
5699 && h->root.u.def.section->output_section == NULL));
5702 /* Array used to determine the number of hash table buckets to use
5703 based on the number of symbols there are. If there are fewer than
5704 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5705 fewer than 37 we use 17 buckets, and so forth. We never use more
5706 than 32771 buckets. */
5708 static const size_t elf_buckets[] =
5710 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5714 /* Compute bucket count for hashing table. We do not use a static set
5715 of possible tables sizes anymore. Instead we determine for all
5716 possible reasonable sizes of the table the outcome (i.e., the
5717 number of collisions etc) and choose the best solution. The
5718 weighting functions are not too simple to allow the table to grow
5719 without bounds. Instead one of the weighting factors is the size.
5720 Therefore the result is always a good payoff between few collisions
5721 (= short chain lengths) and table size. */
5723 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5724 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5725 unsigned long int nsyms,
5728 size_t best_size = 0;
5729 unsigned long int i;
5731 /* We have a problem here. The following code to optimize the table
5732 size requires an integer type with more the 32 bits. If
5733 BFD_HOST_U_64_BIT is set we know about such a type. */
5734 #ifdef BFD_HOST_U_64_BIT
5739 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5740 bfd *dynobj = elf_hash_table (info)->dynobj;
5741 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5742 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5743 unsigned long int *counts;
5745 unsigned int no_improvement_count = 0;
5747 /* Possible optimization parameters: if we have NSYMS symbols we say
5748 that the hashing table must at least have NSYMS/4 and at most
5750 minsize = nsyms / 4;
5753 best_size = maxsize = nsyms * 2;
5758 if ((best_size & 31) == 0)
5762 /* Create array where we count the collisions in. We must use bfd_malloc
5763 since the size could be large. */
5765 amt *= sizeof (unsigned long int);
5766 counts = (unsigned long int *) bfd_malloc (amt);
5770 /* Compute the "optimal" size for the hash table. The criteria is a
5771 minimal chain length. The minor criteria is (of course) the size
5773 for (i = minsize; i < maxsize; ++i)
5775 /* Walk through the array of hashcodes and count the collisions. */
5776 BFD_HOST_U_64_BIT max;
5777 unsigned long int j;
5778 unsigned long int fact;
5780 if (gnu_hash && (i & 31) == 0)
5783 memset (counts, '\0', i * sizeof (unsigned long int));
5785 /* Determine how often each hash bucket is used. */
5786 for (j = 0; j < nsyms; ++j)
5787 ++counts[hashcodes[j] % i];
5789 /* For the weight function we need some information about the
5790 pagesize on the target. This is information need not be 100%
5791 accurate. Since this information is not available (so far) we
5792 define it here to a reasonable default value. If it is crucial
5793 to have a better value some day simply define this value. */
5794 # ifndef BFD_TARGET_PAGESIZE
5795 # define BFD_TARGET_PAGESIZE (4096)
5798 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5800 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5803 /* Variant 1: optimize for short chains. We add the squares
5804 of all the chain lengths (which favors many small chain
5805 over a few long chains). */
5806 for (j = 0; j < i; ++j)
5807 max += counts[j] * counts[j];
5809 /* This adds penalties for the overall size of the table. */
5810 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5813 /* Variant 2: Optimize a lot more for small table. Here we
5814 also add squares of the size but we also add penalties for
5815 empty slots (the +1 term). */
5816 for (j = 0; j < i; ++j)
5817 max += (1 + counts[j]) * (1 + counts[j]);
5819 /* The overall size of the table is considered, but not as
5820 strong as in variant 1, where it is squared. */
5821 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5825 /* Compare with current best results. */
5826 if (max < best_chlen)
5830 no_improvement_count = 0;
5832 /* PR 11843: Avoid futile long searches for the best bucket size
5833 when there are a large number of symbols. */
5834 else if (++no_improvement_count == 100)
5841 #endif /* defined (BFD_HOST_U_64_BIT) */
5843 /* This is the fallback solution if no 64bit type is available or if we
5844 are not supposed to spend much time on optimizations. We select the
5845 bucket count using a fixed set of numbers. */
5846 for (i = 0; elf_buckets[i] != 0; i++)
5848 best_size = elf_buckets[i];
5849 if (nsyms < elf_buckets[i + 1])
5852 if (gnu_hash && best_size < 2)
5859 /* Size any SHT_GROUP section for ld -r. */
5862 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5866 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5867 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5868 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5873 /* Set a default stack segment size. The value in INFO wins. If it
5874 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5875 undefined it is initialized. */
5878 bfd_elf_stack_segment_size (bfd *output_bfd,
5879 struct bfd_link_info *info,
5880 const char *legacy_symbol,
5881 bfd_vma default_size)
5883 struct elf_link_hash_entry *h = NULL;
5885 /* Look for legacy symbol. */
5887 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5888 FALSE, FALSE, FALSE);
5889 if (h && (h->root.type == bfd_link_hash_defined
5890 || h->root.type == bfd_link_hash_defweak)
5892 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5894 /* The symbol has no type if specified on the command line. */
5895 h->type = STT_OBJECT;
5896 if (info->stacksize)
5897 /* xgettext:c-format */
5898 _bfd_error_handler (_("%B: stack size specified and %s set"),
5899 output_bfd, legacy_symbol);
5900 else if (h->root.u.def.section != bfd_abs_section_ptr)
5901 /* xgettext:c-format */
5902 _bfd_error_handler (_("%B: %s not absolute"),
5903 output_bfd, legacy_symbol);
5905 info->stacksize = h->root.u.def.value;
5908 if (!info->stacksize)
5909 /* If the user didn't set a size, or explicitly inhibit the
5910 size, set it now. */
5911 info->stacksize = default_size;
5913 /* Provide the legacy symbol, if it is referenced. */
5914 if (h && (h->root.type == bfd_link_hash_undefined
5915 || h->root.type == bfd_link_hash_undefweak))
5917 struct bfd_link_hash_entry *bh = NULL;
5919 if (!(_bfd_generic_link_add_one_symbol
5920 (info, output_bfd, legacy_symbol,
5921 BSF_GLOBAL, bfd_abs_section_ptr,
5922 info->stacksize >= 0 ? info->stacksize : 0,
5923 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5926 h = (struct elf_link_hash_entry *) bh;
5928 h->type = STT_OBJECT;
5934 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5936 struct elf_gc_sweep_symbol_info
5938 struct bfd_link_info *info;
5939 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5944 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5947 && (((h->root.type == bfd_link_hash_defined
5948 || h->root.type == bfd_link_hash_defweak)
5949 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5950 && h->root.u.def.section->gc_mark))
5951 || h->root.type == bfd_link_hash_undefined
5952 || h->root.type == bfd_link_hash_undefweak))
5954 struct elf_gc_sweep_symbol_info *inf;
5956 inf = (struct elf_gc_sweep_symbol_info *) data;
5957 (*inf->hide_symbol) (inf->info, h, TRUE);
5960 h->ref_regular_nonweak = 0;
5966 /* Set up the sizes and contents of the ELF dynamic sections. This is
5967 called by the ELF linker emulation before_allocation routine. We
5968 must set the sizes of the sections before the linker sets the
5969 addresses of the various sections. */
5972 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5975 const char *filter_shlib,
5977 const char *depaudit,
5978 const char * const *auxiliary_filters,
5979 struct bfd_link_info *info,
5980 asection **sinterpptr)
5983 const struct elf_backend_data *bed;
5987 if (!is_elf_hash_table (info->hash))
5990 dynobj = elf_hash_table (info)->dynobj;
5992 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5994 struct bfd_elf_version_tree *verdefs;
5995 struct elf_info_failed asvinfo;
5996 struct bfd_elf_version_tree *t;
5997 struct bfd_elf_version_expr *d;
5998 struct elf_info_failed eif;
5999 bfd_boolean all_defined;
6006 /* If we are supposed to export all symbols into the dynamic symbol
6007 table (this is not the normal case), then do so. */
6008 if (info->export_dynamic
6009 || (bfd_link_executable (info) && info->dynamic))
6011 elf_link_hash_traverse (elf_hash_table (info),
6012 _bfd_elf_export_symbol,
6020 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6022 if (soname_indx == (size_t) -1
6023 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6027 soname_indx = (size_t) -1;
6029 /* Make all global versions with definition. */
6030 for (t = info->version_info; t != NULL; t = t->next)
6031 for (d = t->globals.list; d != NULL; d = d->next)
6032 if (!d->symver && d->literal)
6034 const char *verstr, *name;
6035 size_t namelen, verlen, newlen;
6036 char *newname, *p, leading_char;
6037 struct elf_link_hash_entry *newh;
6039 leading_char = bfd_get_symbol_leading_char (output_bfd);
6041 namelen = strlen (name) + (leading_char != '\0');
6043 verlen = strlen (verstr);
6044 newlen = namelen + verlen + 3;
6046 newname = (char *) bfd_malloc (newlen);
6047 if (newname == NULL)
6049 newname[0] = leading_char;
6050 memcpy (newname + (leading_char != '\0'), name, namelen);
6052 /* Check the hidden versioned definition. */
6053 p = newname + namelen;
6055 memcpy (p, verstr, verlen + 1);
6056 newh = elf_link_hash_lookup (elf_hash_table (info),
6057 newname, FALSE, FALSE,
6060 || (newh->root.type != bfd_link_hash_defined
6061 && newh->root.type != bfd_link_hash_defweak))
6063 /* Check the default versioned definition. */
6065 memcpy (p, verstr, verlen + 1);
6066 newh = elf_link_hash_lookup (elf_hash_table (info),
6067 newname, FALSE, FALSE,
6072 /* Mark this version if there is a definition and it is
6073 not defined in a shared object. */
6075 && !newh->def_dynamic
6076 && (newh->root.type == bfd_link_hash_defined
6077 || newh->root.type == bfd_link_hash_defweak))
6081 /* Attach all the symbols to their version information. */
6082 asvinfo.info = info;
6083 asvinfo.failed = FALSE;
6085 elf_link_hash_traverse (elf_hash_table (info),
6086 _bfd_elf_link_assign_sym_version,
6091 if (!info->allow_undefined_version)
6093 /* Check if all global versions have a definition. */
6095 for (t = info->version_info; t != NULL; t = t->next)
6096 for (d = t->globals.list; d != NULL; d = d->next)
6097 if (d->literal && !d->symver && !d->script)
6100 (_("%s: undefined version: %s"),
6101 d->pattern, t->name);
6102 all_defined = FALSE;
6107 bfd_set_error (bfd_error_bad_value);
6112 /* Set up the version definition section. */
6113 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6114 BFD_ASSERT (s != NULL);
6116 /* We may have created additional version definitions if we are
6117 just linking a regular application. */
6118 verdefs = info->version_info;
6120 /* Skip anonymous version tag. */
6121 if (verdefs != NULL && verdefs->vernum == 0)
6122 verdefs = verdefs->next;
6124 if (verdefs == NULL && !info->create_default_symver)
6125 s->flags |= SEC_EXCLUDE;
6131 Elf_Internal_Verdef def;
6132 Elf_Internal_Verdaux defaux;
6133 struct bfd_link_hash_entry *bh;
6134 struct elf_link_hash_entry *h;
6140 /* Make space for the base version. */
6141 size += sizeof (Elf_External_Verdef);
6142 size += sizeof (Elf_External_Verdaux);
6145 /* Make space for the default version. */
6146 if (info->create_default_symver)
6148 size += sizeof (Elf_External_Verdef);
6152 for (t = verdefs; t != NULL; t = t->next)
6154 struct bfd_elf_version_deps *n;
6156 /* Don't emit base version twice. */
6160 size += sizeof (Elf_External_Verdef);
6161 size += sizeof (Elf_External_Verdaux);
6164 for (n = t->deps; n != NULL; n = n->next)
6165 size += sizeof (Elf_External_Verdaux);
6169 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6170 if (s->contents == NULL && s->size != 0)
6173 /* Fill in the version definition section. */
6177 def.vd_version = VER_DEF_CURRENT;
6178 def.vd_flags = VER_FLG_BASE;
6181 if (info->create_default_symver)
6183 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6184 def.vd_next = sizeof (Elf_External_Verdef);
6188 def.vd_aux = sizeof (Elf_External_Verdef);
6189 def.vd_next = (sizeof (Elf_External_Verdef)
6190 + sizeof (Elf_External_Verdaux));
6193 if (soname_indx != (size_t) -1)
6195 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6197 def.vd_hash = bfd_elf_hash (soname);
6198 defaux.vda_name = soname_indx;
6205 name = lbasename (output_bfd->filename);
6206 def.vd_hash = bfd_elf_hash (name);
6207 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6209 if (indx == (size_t) -1)
6211 defaux.vda_name = indx;
6213 defaux.vda_next = 0;
6215 _bfd_elf_swap_verdef_out (output_bfd, &def,
6216 (Elf_External_Verdef *) p);
6217 p += sizeof (Elf_External_Verdef);
6218 if (info->create_default_symver)
6220 /* Add a symbol representing this version. */
6222 if (! (_bfd_generic_link_add_one_symbol
6223 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6225 get_elf_backend_data (dynobj)->collect, &bh)))
6227 h = (struct elf_link_hash_entry *) bh;
6230 h->type = STT_OBJECT;
6231 h->verinfo.vertree = NULL;
6233 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6236 /* Create a duplicate of the base version with the same
6237 aux block, but different flags. */
6240 def.vd_aux = sizeof (Elf_External_Verdef);
6242 def.vd_next = (sizeof (Elf_External_Verdef)
6243 + sizeof (Elf_External_Verdaux));
6246 _bfd_elf_swap_verdef_out (output_bfd, &def,
6247 (Elf_External_Verdef *) p);
6248 p += sizeof (Elf_External_Verdef);
6250 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6251 (Elf_External_Verdaux *) p);
6252 p += sizeof (Elf_External_Verdaux);
6254 for (t = verdefs; t != NULL; t = t->next)
6257 struct bfd_elf_version_deps *n;
6259 /* Don't emit the base version twice. */
6264 for (n = t->deps; n != NULL; n = n->next)
6267 /* Add a symbol representing this version. */
6269 if (! (_bfd_generic_link_add_one_symbol
6270 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6272 get_elf_backend_data (dynobj)->collect, &bh)))
6274 h = (struct elf_link_hash_entry *) bh;
6277 h->type = STT_OBJECT;
6278 h->verinfo.vertree = t;
6280 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6283 def.vd_version = VER_DEF_CURRENT;
6285 if (t->globals.list == NULL
6286 && t->locals.list == NULL
6288 def.vd_flags |= VER_FLG_WEAK;
6289 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6290 def.vd_cnt = cdeps + 1;
6291 def.vd_hash = bfd_elf_hash (t->name);
6292 def.vd_aux = sizeof (Elf_External_Verdef);
6295 /* If a basever node is next, it *must* be the last node in
6296 the chain, otherwise Verdef construction breaks. */
6297 if (t->next != NULL && t->next->vernum == 0)
6298 BFD_ASSERT (t->next->next == NULL);
6300 if (t->next != NULL && t->next->vernum != 0)
6301 def.vd_next = (sizeof (Elf_External_Verdef)
6302 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6304 _bfd_elf_swap_verdef_out (output_bfd, &def,
6305 (Elf_External_Verdef *) p);
6306 p += sizeof (Elf_External_Verdef);
6308 defaux.vda_name = h->dynstr_index;
6309 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6311 defaux.vda_next = 0;
6312 if (t->deps != NULL)
6313 defaux.vda_next = sizeof (Elf_External_Verdaux);
6314 t->name_indx = defaux.vda_name;
6316 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6317 (Elf_External_Verdaux *) p);
6318 p += sizeof (Elf_External_Verdaux);
6320 for (n = t->deps; n != NULL; n = n->next)
6322 if (n->version_needed == NULL)
6324 /* This can happen if there was an error in the
6326 defaux.vda_name = 0;
6330 defaux.vda_name = n->version_needed->name_indx;
6331 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6334 if (n->next == NULL)
6335 defaux.vda_next = 0;
6337 defaux.vda_next = sizeof (Elf_External_Verdaux);
6339 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6340 (Elf_External_Verdaux *) p);
6341 p += sizeof (Elf_External_Verdaux);
6345 elf_tdata (output_bfd)->cverdefs = cdefs;
6348 /* Work out the size of the version reference section. */
6350 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6351 BFD_ASSERT (s != NULL);
6353 struct elf_find_verdep_info sinfo;
6356 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6357 if (sinfo.vers == 0)
6359 sinfo.failed = FALSE;
6361 elf_link_hash_traverse (elf_hash_table (info),
6362 _bfd_elf_link_find_version_dependencies,
6367 if (elf_tdata (output_bfd)->verref == NULL)
6368 s->flags |= SEC_EXCLUDE;
6371 Elf_Internal_Verneed *vn;
6376 /* Build the version dependency section. */
6379 for (vn = elf_tdata (output_bfd)->verref;
6381 vn = vn->vn_nextref)
6383 Elf_Internal_Vernaux *a;
6385 size += sizeof (Elf_External_Verneed);
6387 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6388 size += sizeof (Elf_External_Vernaux);
6392 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6393 if (s->contents == NULL)
6397 for (vn = elf_tdata (output_bfd)->verref;
6399 vn = vn->vn_nextref)
6402 Elf_Internal_Vernaux *a;
6406 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6409 vn->vn_version = VER_NEED_CURRENT;
6411 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6412 elf_dt_name (vn->vn_bfd) != NULL
6413 ? elf_dt_name (vn->vn_bfd)
6414 : lbasename (vn->vn_bfd->filename),
6416 if (indx == (size_t) -1)
6419 vn->vn_aux = sizeof (Elf_External_Verneed);
6420 if (vn->vn_nextref == NULL)
6423 vn->vn_next = (sizeof (Elf_External_Verneed)
6424 + caux * sizeof (Elf_External_Vernaux));
6426 _bfd_elf_swap_verneed_out (output_bfd, vn,
6427 (Elf_External_Verneed *) p);
6428 p += sizeof (Elf_External_Verneed);
6430 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6432 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6433 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6434 a->vna_nodename, FALSE);
6435 if (indx == (size_t) -1)
6438 if (a->vna_nextptr == NULL)
6441 a->vna_next = sizeof (Elf_External_Vernaux);
6443 _bfd_elf_swap_vernaux_out (output_bfd, a,
6444 (Elf_External_Vernaux *) p);
6445 p += sizeof (Elf_External_Vernaux);
6449 elf_tdata (output_bfd)->cverrefs = crefs;
6454 bed = get_elf_backend_data (output_bfd);
6456 if (info->gc_sections && bed->can_gc_sections)
6458 struct elf_gc_sweep_symbol_info sweep_info;
6459 unsigned long section_sym_count;
6461 /* Remove the symbols that were in the swept sections from the
6462 dynamic symbol table. GCFIXME: Anyone know how to get them
6463 out of the static symbol table as well? */
6464 sweep_info.info = info;
6465 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6466 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6469 /* We need to reassign dynsym indices now that symbols may have
6470 been removed. See the call in `bfd_elf_size_dynsym_hash_dynstr'
6471 for the details of the conditions used here. */
6472 if (elf_hash_table (info)->dynamic_sections_created
6473 || bed->always_renumber_dynsyms)
6474 _bfd_elf_link_renumber_dynsyms (output_bfd, info, §ion_sym_count);
6477 /* Any syms created from now on start with -1 in
6478 got.refcount/offset and plt.refcount/offset. */
6479 elf_hash_table (info)->init_got_refcount
6480 = elf_hash_table (info)->init_got_offset;
6481 elf_hash_table (info)->init_plt_refcount
6482 = elf_hash_table (info)->init_plt_offset;
6484 if (bfd_link_relocatable (info)
6485 && !_bfd_elf_size_group_sections (info))
6488 /* The backend may have to create some sections regardless of whether
6489 we're dynamic or not. */
6490 if (bed->elf_backend_always_size_sections
6491 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6494 /* Determine any GNU_STACK segment requirements, after the backend
6495 has had a chance to set a default segment size. */
6496 if (info->execstack)
6497 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6498 else if (info->noexecstack)
6499 elf_stack_flags (output_bfd) = PF_R | PF_W;
6503 asection *notesec = NULL;
6506 for (inputobj = info->input_bfds;
6508 inputobj = inputobj->link.next)
6513 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6515 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6518 if (s->flags & SEC_CODE)
6522 else if (bed->default_execstack)
6525 if (notesec || info->stacksize > 0)
6526 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6527 if (notesec && exec && bfd_link_relocatable (info)
6528 && notesec->output_section != bfd_abs_section_ptr)
6529 notesec->output_section->flags |= SEC_CODE;
6532 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6534 struct elf_info_failed eif;
6535 struct elf_link_hash_entry *h;
6539 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6540 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6544 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6546 info->flags |= DF_SYMBOLIC;
6554 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6556 if (indx == (size_t) -1)
6559 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6560 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6564 if (filter_shlib != NULL)
6568 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6569 filter_shlib, TRUE);
6570 if (indx == (size_t) -1
6571 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6575 if (auxiliary_filters != NULL)
6577 const char * const *p;
6579 for (p = auxiliary_filters; *p != NULL; p++)
6583 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6585 if (indx == (size_t) -1
6586 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6595 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6597 if (indx == (size_t) -1
6598 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6602 if (depaudit != NULL)
6606 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6608 if (indx == (size_t) -1
6609 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6616 /* Find all symbols which were defined in a dynamic object and make
6617 the backend pick a reasonable value for them. */
6618 elf_link_hash_traverse (elf_hash_table (info),
6619 _bfd_elf_adjust_dynamic_symbol,
6624 /* Add some entries to the .dynamic section. We fill in some of the
6625 values later, in bfd_elf_final_link, but we must add the entries
6626 now so that we know the final size of the .dynamic section. */
6628 /* If there are initialization and/or finalization functions to
6629 call then add the corresponding DT_INIT/DT_FINI entries. */
6630 h = (info->init_function
6631 ? elf_link_hash_lookup (elf_hash_table (info),
6632 info->init_function, FALSE,
6639 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6642 h = (info->fini_function
6643 ? elf_link_hash_lookup (elf_hash_table (info),
6644 info->fini_function, FALSE,
6651 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6655 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6656 if (s != NULL && s->linker_has_input)
6658 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6659 if (! bfd_link_executable (info))
6664 for (sub = info->input_bfds; sub != NULL;
6665 sub = sub->link.next)
6666 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6667 for (o = sub->sections; o != NULL; o = o->next)
6668 if (elf_section_data (o)->this_hdr.sh_type
6669 == SHT_PREINIT_ARRAY)
6672 (_("%B: .preinit_array section is not allowed in DSO"),
6677 bfd_set_error (bfd_error_nonrepresentable_section);
6681 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6682 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6685 s = bfd_get_section_by_name (output_bfd, ".init_array");
6686 if (s != NULL && s->linker_has_input)
6688 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6689 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6692 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6693 if (s != NULL && s->linker_has_input)
6695 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6696 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6700 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6701 /* If .dynstr is excluded from the link, we don't want any of
6702 these tags. Strictly, we should be checking each section
6703 individually; This quick check covers for the case where
6704 someone does a /DISCARD/ : { *(*) }. */
6705 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6707 bfd_size_type strsize;
6709 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6710 if ((info->emit_hash
6711 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6712 || (info->emit_gnu_hash
6713 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6714 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6715 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6716 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6717 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6718 bed->s->sizeof_sym))
6723 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6726 /* The backend must work out the sizes of all the other dynamic
6729 && bed->elf_backend_size_dynamic_sections != NULL
6730 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6733 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6735 unsigned long section_sym_count;
6737 if (elf_tdata (output_bfd)->cverdefs)
6739 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6741 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6742 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6746 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6748 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6751 else if (info->flags & DF_BIND_NOW)
6753 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6759 if (bfd_link_executable (info))
6760 info->flags_1 &= ~ (DF_1_INITFIRST
6763 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6767 if (elf_tdata (output_bfd)->cverrefs)
6769 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6771 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6772 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6776 if ((elf_tdata (output_bfd)->cverrefs == 0
6777 && elf_tdata (output_bfd)->cverdefs == 0)
6778 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6779 §ion_sym_count) == 0)
6783 s = bfd_get_linker_section (dynobj, ".gnu.version");
6784 s->flags |= SEC_EXCLUDE;
6790 /* Find the first non-excluded output section. We'll use its
6791 section symbol for some emitted relocs. */
6793 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6797 for (s = output_bfd->sections; s != NULL; s = s->next)
6798 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6799 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6801 elf_hash_table (info)->text_index_section = s;
6806 /* Find two non-excluded output sections, one for code, one for data.
6807 We'll use their section symbols for some emitted relocs. */
6809 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6813 /* Data first, since setting text_index_section changes
6814 _bfd_elf_link_omit_section_dynsym. */
6815 for (s = output_bfd->sections; s != NULL; s = s->next)
6816 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6817 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6819 elf_hash_table (info)->data_index_section = s;
6823 for (s = output_bfd->sections; s != NULL; s = s->next)
6824 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6825 == (SEC_ALLOC | SEC_READONLY))
6826 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6828 elf_hash_table (info)->text_index_section = s;
6832 if (elf_hash_table (info)->text_index_section == NULL)
6833 elf_hash_table (info)->text_index_section
6834 = elf_hash_table (info)->data_index_section;
6838 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6840 const struct elf_backend_data *bed;
6841 unsigned long section_sym_count;
6842 bfd_size_type dynsymcount;
6844 if (!is_elf_hash_table (info->hash))
6847 bed = get_elf_backend_data (output_bfd);
6848 (*bed->elf_backend_init_index_section) (output_bfd, info);
6850 /* Assign dynsym indices. In a shared library we generate a section
6851 symbol for each output section, which come first. Next come all
6852 of the back-end allocated local dynamic syms, followed by the rest
6853 of the global symbols.
6855 This is usually not needed for static binaries, however backends
6856 can request to always do it, e.g. the MIPS backend uses dynamic
6857 symbol counts to lay out GOT, which will be produced in the
6858 presence of GOT relocations even in static binaries (holding fixed
6859 data in that case, to satisfy those relocations). */
6861 if (elf_hash_table (info)->dynamic_sections_created
6862 || bed->always_renumber_dynsyms)
6863 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6864 §ion_sym_count);
6866 if (elf_hash_table (info)->dynamic_sections_created)
6870 unsigned int dtagcount;
6872 dynobj = elf_hash_table (info)->dynobj;
6874 /* Work out the size of the symbol version section. */
6875 s = bfd_get_linker_section (dynobj, ".gnu.version");
6876 BFD_ASSERT (s != NULL);
6877 if ((s->flags & SEC_EXCLUDE) == 0)
6879 s->size = dynsymcount * sizeof (Elf_External_Versym);
6880 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6881 if (s->contents == NULL)
6884 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6888 /* Set the size of the .dynsym and .hash sections. We counted
6889 the number of dynamic symbols in elf_link_add_object_symbols.
6890 We will build the contents of .dynsym and .hash when we build
6891 the final symbol table, because until then we do not know the
6892 correct value to give the symbols. We built the .dynstr
6893 section as we went along in elf_link_add_object_symbols. */
6894 s = elf_hash_table (info)->dynsym;
6895 BFD_ASSERT (s != NULL);
6896 s->size = dynsymcount * bed->s->sizeof_sym;
6898 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6899 if (s->contents == NULL)
6902 /* The first entry in .dynsym is a dummy symbol. Clear all the
6903 section syms, in case we don't output them all. */
6904 ++section_sym_count;
6905 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6907 elf_hash_table (info)->bucketcount = 0;
6909 /* Compute the size of the hashing table. As a side effect this
6910 computes the hash values for all the names we export. */
6911 if (info->emit_hash)
6913 unsigned long int *hashcodes;
6914 struct hash_codes_info hashinf;
6916 unsigned long int nsyms;
6918 size_t hash_entry_size;
6920 /* Compute the hash values for all exported symbols. At the same
6921 time store the values in an array so that we could use them for
6923 amt = dynsymcount * sizeof (unsigned long int);
6924 hashcodes = (unsigned long int *) bfd_malloc (amt);
6925 if (hashcodes == NULL)
6927 hashinf.hashcodes = hashcodes;
6928 hashinf.error = FALSE;
6930 /* Put all hash values in HASHCODES. */
6931 elf_link_hash_traverse (elf_hash_table (info),
6932 elf_collect_hash_codes, &hashinf);
6939 nsyms = hashinf.hashcodes - hashcodes;
6941 = compute_bucket_count (info, hashcodes, nsyms, 0);
6944 if (bucketcount == 0)
6947 elf_hash_table (info)->bucketcount = bucketcount;
6949 s = bfd_get_linker_section (dynobj, ".hash");
6950 BFD_ASSERT (s != NULL);
6951 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6952 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6953 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6954 if (s->contents == NULL)
6957 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6958 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6959 s->contents + hash_entry_size);
6962 if (info->emit_gnu_hash)
6965 unsigned char *contents;
6966 struct collect_gnu_hash_codes cinfo;
6970 memset (&cinfo, 0, sizeof (cinfo));
6972 /* Compute the hash values for all exported symbols. At the same
6973 time store the values in an array so that we could use them for
6975 amt = dynsymcount * 2 * sizeof (unsigned long int);
6976 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6977 if (cinfo.hashcodes == NULL)
6980 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6981 cinfo.min_dynindx = -1;
6982 cinfo.output_bfd = output_bfd;
6985 /* Put all hash values in HASHCODES. */
6986 elf_link_hash_traverse (elf_hash_table (info),
6987 elf_collect_gnu_hash_codes, &cinfo);
6990 free (cinfo.hashcodes);
6995 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6997 if (bucketcount == 0)
6999 free (cinfo.hashcodes);
7003 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7004 BFD_ASSERT (s != NULL);
7006 if (cinfo.nsyms == 0)
7008 /* Empty .gnu.hash section is special. */
7009 BFD_ASSERT (cinfo.min_dynindx == -1);
7010 free (cinfo.hashcodes);
7011 s->size = 5 * 4 + bed->s->arch_size / 8;
7012 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7013 if (contents == NULL)
7015 s->contents = contents;
7016 /* 1 empty bucket. */
7017 bfd_put_32 (output_bfd, 1, contents);
7018 /* SYMIDX above the special symbol 0. */
7019 bfd_put_32 (output_bfd, 1, contents + 4);
7020 /* Just one word for bitmask. */
7021 bfd_put_32 (output_bfd, 1, contents + 8);
7022 /* Only hash fn bloom filter. */
7023 bfd_put_32 (output_bfd, 0, contents + 12);
7024 /* No hashes are valid - empty bitmask. */
7025 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7026 /* No hashes in the only bucket. */
7027 bfd_put_32 (output_bfd, 0,
7028 contents + 16 + bed->s->arch_size / 8);
7032 unsigned long int maskwords, maskbitslog2, x;
7033 BFD_ASSERT (cinfo.min_dynindx != -1);
7037 while ((x >>= 1) != 0)
7039 if (maskbitslog2 < 3)
7041 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7042 maskbitslog2 = maskbitslog2 + 3;
7044 maskbitslog2 = maskbitslog2 + 2;
7045 if (bed->s->arch_size == 64)
7047 if (maskbitslog2 == 5)
7053 cinfo.mask = (1 << cinfo.shift1) - 1;
7054 cinfo.shift2 = maskbitslog2;
7055 cinfo.maskbits = 1 << maskbitslog2;
7056 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7057 amt = bucketcount * sizeof (unsigned long int) * 2;
7058 amt += maskwords * sizeof (bfd_vma);
7059 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7060 if (cinfo.bitmask == NULL)
7062 free (cinfo.hashcodes);
7066 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7067 cinfo.indx = cinfo.counts + bucketcount;
7068 cinfo.symindx = dynsymcount - cinfo.nsyms;
7069 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7071 /* Determine how often each hash bucket is used. */
7072 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7073 for (i = 0; i < cinfo.nsyms; ++i)
7074 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7076 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7077 if (cinfo.counts[i] != 0)
7079 cinfo.indx[i] = cnt;
7080 cnt += cinfo.counts[i];
7082 BFD_ASSERT (cnt == dynsymcount);
7083 cinfo.bucketcount = bucketcount;
7084 cinfo.local_indx = cinfo.min_dynindx;
7086 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7087 s->size += cinfo.maskbits / 8;
7088 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7089 if (contents == NULL)
7091 free (cinfo.bitmask);
7092 free (cinfo.hashcodes);
7096 s->contents = contents;
7097 bfd_put_32 (output_bfd, bucketcount, contents);
7098 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7099 bfd_put_32 (output_bfd, maskwords, contents + 8);
7100 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7101 contents += 16 + cinfo.maskbits / 8;
7103 for (i = 0; i < bucketcount; ++i)
7105 if (cinfo.counts[i] == 0)
7106 bfd_put_32 (output_bfd, 0, contents);
7108 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7112 cinfo.contents = contents;
7114 /* Renumber dynamic symbols, populate .gnu.hash section. */
7115 elf_link_hash_traverse (elf_hash_table (info),
7116 elf_renumber_gnu_hash_syms, &cinfo);
7118 contents = s->contents + 16;
7119 for (i = 0; i < maskwords; ++i)
7121 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7123 contents += bed->s->arch_size / 8;
7126 free (cinfo.bitmask);
7127 free (cinfo.hashcodes);
7131 s = bfd_get_linker_section (dynobj, ".dynstr");
7132 BFD_ASSERT (s != NULL);
7134 elf_finalize_dynstr (output_bfd, info);
7136 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7138 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7139 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7146 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7149 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7152 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7153 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7156 /* Finish SHF_MERGE section merging. */
7159 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7164 if (!is_elf_hash_table (info->hash))
7167 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7168 if ((ibfd->flags & DYNAMIC) == 0
7169 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7170 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7171 == get_elf_backend_data (obfd)->s->elfclass))
7172 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7173 if ((sec->flags & SEC_MERGE) != 0
7174 && !bfd_is_abs_section (sec->output_section))
7176 struct bfd_elf_section_data *secdata;
7178 secdata = elf_section_data (sec);
7179 if (! _bfd_add_merge_section (obfd,
7180 &elf_hash_table (info)->merge_info,
7181 sec, &secdata->sec_info))
7183 else if (secdata->sec_info)
7184 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7187 if (elf_hash_table (info)->merge_info != NULL)
7188 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7189 merge_sections_remove_hook);
7193 /* Create an entry in an ELF linker hash table. */
7195 struct bfd_hash_entry *
7196 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7197 struct bfd_hash_table *table,
7200 /* Allocate the structure if it has not already been allocated by a
7204 entry = (struct bfd_hash_entry *)
7205 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7210 /* Call the allocation method of the superclass. */
7211 entry = _bfd_link_hash_newfunc (entry, table, string);
7214 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7215 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7217 /* Set local fields. */
7220 ret->got = htab->init_got_refcount;
7221 ret->plt = htab->init_plt_refcount;
7222 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7223 - offsetof (struct elf_link_hash_entry, size)));
7224 /* Assume that we have been called by a non-ELF symbol reader.
7225 This flag is then reset by the code which reads an ELF input
7226 file. This ensures that a symbol created by a non-ELF symbol
7227 reader will have the flag set correctly. */
7234 /* Copy data from an indirect symbol to its direct symbol, hiding the
7235 old indirect symbol. Also used for copying flags to a weakdef. */
7238 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7239 struct elf_link_hash_entry *dir,
7240 struct elf_link_hash_entry *ind)
7242 struct elf_link_hash_table *htab;
7244 /* Copy down any references that we may have already seen to the
7245 symbol which just became indirect. */
7247 if (dir->versioned != versioned_hidden)
7248 dir->ref_dynamic |= ind->ref_dynamic;
7249 dir->ref_regular |= ind->ref_regular;
7250 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7251 dir->non_got_ref |= ind->non_got_ref;
7252 dir->needs_plt |= ind->needs_plt;
7253 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7255 if (ind->root.type != bfd_link_hash_indirect)
7258 /* Copy over the global and procedure linkage table refcount entries.
7259 These may have been already set up by a check_relocs routine. */
7260 htab = elf_hash_table (info);
7261 if (ind->got.refcount > htab->init_got_refcount.refcount)
7263 if (dir->got.refcount < 0)
7264 dir->got.refcount = 0;
7265 dir->got.refcount += ind->got.refcount;
7266 ind->got.refcount = htab->init_got_refcount.refcount;
7269 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7271 if (dir->plt.refcount < 0)
7272 dir->plt.refcount = 0;
7273 dir->plt.refcount += ind->plt.refcount;
7274 ind->plt.refcount = htab->init_plt_refcount.refcount;
7277 if (ind->dynindx != -1)
7279 if (dir->dynindx != -1)
7280 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7281 dir->dynindx = ind->dynindx;
7282 dir->dynstr_index = ind->dynstr_index;
7284 ind->dynstr_index = 0;
7289 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7290 struct elf_link_hash_entry *h,
7291 bfd_boolean force_local)
7293 /* STT_GNU_IFUNC symbol must go through PLT. */
7294 if (h->type != STT_GNU_IFUNC)
7296 h->plt = elf_hash_table (info)->init_plt_offset;
7301 h->forced_local = 1;
7302 if (h->dynindx != -1)
7304 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7307 h->dynstr_index = 0;
7312 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7316 _bfd_elf_link_hash_table_init
7317 (struct elf_link_hash_table *table,
7319 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7320 struct bfd_hash_table *,
7322 unsigned int entsize,
7323 enum elf_target_id target_id)
7326 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7328 table->init_got_refcount.refcount = can_refcount - 1;
7329 table->init_plt_refcount.refcount = can_refcount - 1;
7330 table->init_got_offset.offset = -(bfd_vma) 1;
7331 table->init_plt_offset.offset = -(bfd_vma) 1;
7332 /* The first dynamic symbol is a dummy. */
7333 table->dynsymcount = 1;
7335 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7337 table->root.type = bfd_link_elf_hash_table;
7338 table->hash_table_id = target_id;
7343 /* Create an ELF linker hash table. */
7345 struct bfd_link_hash_table *
7346 _bfd_elf_link_hash_table_create (bfd *abfd)
7348 struct elf_link_hash_table *ret;
7349 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7351 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7355 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7356 sizeof (struct elf_link_hash_entry),
7362 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7367 /* Destroy an ELF linker hash table. */
7370 _bfd_elf_link_hash_table_free (bfd *obfd)
7372 struct elf_link_hash_table *htab;
7374 htab = (struct elf_link_hash_table *) obfd->link.hash;
7375 if (htab->dynstr != NULL)
7376 _bfd_elf_strtab_free (htab->dynstr);
7377 _bfd_merge_sections_free (htab->merge_info);
7378 _bfd_generic_link_hash_table_free (obfd);
7381 /* This is a hook for the ELF emulation code in the generic linker to
7382 tell the backend linker what file name to use for the DT_NEEDED
7383 entry for a dynamic object. */
7386 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7388 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7389 && bfd_get_format (abfd) == bfd_object)
7390 elf_dt_name (abfd) = name;
7394 bfd_elf_get_dyn_lib_class (bfd *abfd)
7397 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7398 && bfd_get_format (abfd) == bfd_object)
7399 lib_class = elf_dyn_lib_class (abfd);
7406 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7408 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7409 && bfd_get_format (abfd) == bfd_object)
7410 elf_dyn_lib_class (abfd) = lib_class;
7413 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7414 the linker ELF emulation code. */
7416 struct bfd_link_needed_list *
7417 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7418 struct bfd_link_info *info)
7420 if (! is_elf_hash_table (info->hash))
7422 return elf_hash_table (info)->needed;
7425 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7426 hook for the linker ELF emulation code. */
7428 struct bfd_link_needed_list *
7429 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7430 struct bfd_link_info *info)
7432 if (! is_elf_hash_table (info->hash))
7434 return elf_hash_table (info)->runpath;
7437 /* Get the name actually used for a dynamic object for a link. This
7438 is the SONAME entry if there is one. Otherwise, it is the string
7439 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7442 bfd_elf_get_dt_soname (bfd *abfd)
7444 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7445 && bfd_get_format (abfd) == bfd_object)
7446 return elf_dt_name (abfd);
7450 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7451 the ELF linker emulation code. */
7454 bfd_elf_get_bfd_needed_list (bfd *abfd,
7455 struct bfd_link_needed_list **pneeded)
7458 bfd_byte *dynbuf = NULL;
7459 unsigned int elfsec;
7460 unsigned long shlink;
7461 bfd_byte *extdyn, *extdynend;
7463 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7467 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7468 || bfd_get_format (abfd) != bfd_object)
7471 s = bfd_get_section_by_name (abfd, ".dynamic");
7472 if (s == NULL || s->size == 0)
7475 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7478 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7479 if (elfsec == SHN_BAD)
7482 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7484 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7485 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7488 extdynend = extdyn + s->size;
7489 for (; extdyn < extdynend; extdyn += extdynsize)
7491 Elf_Internal_Dyn dyn;
7493 (*swap_dyn_in) (abfd, extdyn, &dyn);
7495 if (dyn.d_tag == DT_NULL)
7498 if (dyn.d_tag == DT_NEEDED)
7501 struct bfd_link_needed_list *l;
7502 unsigned int tagv = dyn.d_un.d_val;
7505 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7510 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7531 struct elf_symbuf_symbol
7533 unsigned long st_name; /* Symbol name, index in string tbl */
7534 unsigned char st_info; /* Type and binding attributes */
7535 unsigned char st_other; /* Visibilty, and target specific */
7538 struct elf_symbuf_head
7540 struct elf_symbuf_symbol *ssym;
7542 unsigned int st_shndx;
7549 Elf_Internal_Sym *isym;
7550 struct elf_symbuf_symbol *ssym;
7555 /* Sort references to symbols by ascending section number. */
7558 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7560 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7561 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7563 return s1->st_shndx - s2->st_shndx;
7567 elf_sym_name_compare (const void *arg1, const void *arg2)
7569 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7570 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7571 return strcmp (s1->name, s2->name);
7574 static struct elf_symbuf_head *
7575 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7577 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7578 struct elf_symbuf_symbol *ssym;
7579 struct elf_symbuf_head *ssymbuf, *ssymhead;
7580 size_t i, shndx_count, total_size;
7582 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7586 for (ind = indbuf, i = 0; i < symcount; i++)
7587 if (isymbuf[i].st_shndx != SHN_UNDEF)
7588 *ind++ = &isymbuf[i];
7591 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7592 elf_sort_elf_symbol);
7595 if (indbufend > indbuf)
7596 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7597 if (ind[0]->st_shndx != ind[1]->st_shndx)
7600 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7601 + (indbufend - indbuf) * sizeof (*ssym));
7602 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7603 if (ssymbuf == NULL)
7609 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7610 ssymbuf->ssym = NULL;
7611 ssymbuf->count = shndx_count;
7612 ssymbuf->st_shndx = 0;
7613 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7615 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7618 ssymhead->ssym = ssym;
7619 ssymhead->count = 0;
7620 ssymhead->st_shndx = (*ind)->st_shndx;
7622 ssym->st_name = (*ind)->st_name;
7623 ssym->st_info = (*ind)->st_info;
7624 ssym->st_other = (*ind)->st_other;
7627 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7628 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7635 /* Check if 2 sections define the same set of local and global
7639 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7640 struct bfd_link_info *info)
7643 const struct elf_backend_data *bed1, *bed2;
7644 Elf_Internal_Shdr *hdr1, *hdr2;
7645 size_t symcount1, symcount2;
7646 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7647 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7648 Elf_Internal_Sym *isym, *isymend;
7649 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7650 size_t count1, count2, i;
7651 unsigned int shndx1, shndx2;
7657 /* Both sections have to be in ELF. */
7658 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7659 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7662 if (elf_section_type (sec1) != elf_section_type (sec2))
7665 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7666 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7667 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7670 bed1 = get_elf_backend_data (bfd1);
7671 bed2 = get_elf_backend_data (bfd2);
7672 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7673 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7674 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7675 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7677 if (symcount1 == 0 || symcount2 == 0)
7683 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7684 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7686 if (ssymbuf1 == NULL)
7688 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7690 if (isymbuf1 == NULL)
7693 if (!info->reduce_memory_overheads)
7694 elf_tdata (bfd1)->symbuf = ssymbuf1
7695 = elf_create_symbuf (symcount1, isymbuf1);
7698 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7700 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7702 if (isymbuf2 == NULL)
7705 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7706 elf_tdata (bfd2)->symbuf = ssymbuf2
7707 = elf_create_symbuf (symcount2, isymbuf2);
7710 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7712 /* Optimized faster version. */
7714 struct elf_symbol *symp;
7715 struct elf_symbuf_symbol *ssym, *ssymend;
7718 hi = ssymbuf1->count;
7723 mid = (lo + hi) / 2;
7724 if (shndx1 < ssymbuf1[mid].st_shndx)
7726 else if (shndx1 > ssymbuf1[mid].st_shndx)
7730 count1 = ssymbuf1[mid].count;
7737 hi = ssymbuf2->count;
7742 mid = (lo + hi) / 2;
7743 if (shndx2 < ssymbuf2[mid].st_shndx)
7745 else if (shndx2 > ssymbuf2[mid].st_shndx)
7749 count2 = ssymbuf2[mid].count;
7755 if (count1 == 0 || count2 == 0 || count1 != count2)
7759 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7761 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7762 if (symtable1 == NULL || symtable2 == NULL)
7766 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7767 ssym < ssymend; ssym++, symp++)
7769 symp->u.ssym = ssym;
7770 symp->name = bfd_elf_string_from_elf_section (bfd1,
7776 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7777 ssym < ssymend; ssym++, symp++)
7779 symp->u.ssym = ssym;
7780 symp->name = bfd_elf_string_from_elf_section (bfd2,
7785 /* Sort symbol by name. */
7786 qsort (symtable1, count1, sizeof (struct elf_symbol),
7787 elf_sym_name_compare);
7788 qsort (symtable2, count1, sizeof (struct elf_symbol),
7789 elf_sym_name_compare);
7791 for (i = 0; i < count1; i++)
7792 /* Two symbols must have the same binding, type and name. */
7793 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7794 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7795 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7802 symtable1 = (struct elf_symbol *)
7803 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7804 symtable2 = (struct elf_symbol *)
7805 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7806 if (symtable1 == NULL || symtable2 == NULL)
7809 /* Count definitions in the section. */
7811 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7812 if (isym->st_shndx == shndx1)
7813 symtable1[count1++].u.isym = isym;
7816 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7817 if (isym->st_shndx == shndx2)
7818 symtable2[count2++].u.isym = isym;
7820 if (count1 == 0 || count2 == 0 || count1 != count2)
7823 for (i = 0; i < count1; i++)
7825 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7826 symtable1[i].u.isym->st_name);
7828 for (i = 0; i < count2; i++)
7830 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7831 symtable2[i].u.isym->st_name);
7833 /* Sort symbol by name. */
7834 qsort (symtable1, count1, sizeof (struct elf_symbol),
7835 elf_sym_name_compare);
7836 qsort (symtable2, count1, sizeof (struct elf_symbol),
7837 elf_sym_name_compare);
7839 for (i = 0; i < count1; i++)
7840 /* Two symbols must have the same binding, type and name. */
7841 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7842 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7843 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7861 /* Return TRUE if 2 section types are compatible. */
7864 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7865 bfd *bbfd, const asection *bsec)
7869 || abfd->xvec->flavour != bfd_target_elf_flavour
7870 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7873 return elf_section_type (asec) == elf_section_type (bsec);
7876 /* Final phase of ELF linker. */
7878 /* A structure we use to avoid passing large numbers of arguments. */
7880 struct elf_final_link_info
7882 /* General link information. */
7883 struct bfd_link_info *info;
7886 /* Symbol string table. */
7887 struct elf_strtab_hash *symstrtab;
7888 /* .hash section. */
7890 /* symbol version section (.gnu.version). */
7891 asection *symver_sec;
7892 /* Buffer large enough to hold contents of any section. */
7894 /* Buffer large enough to hold external relocs of any section. */
7895 void *external_relocs;
7896 /* Buffer large enough to hold internal relocs of any section. */
7897 Elf_Internal_Rela *internal_relocs;
7898 /* Buffer large enough to hold external local symbols of any input
7900 bfd_byte *external_syms;
7901 /* And a buffer for symbol section indices. */
7902 Elf_External_Sym_Shndx *locsym_shndx;
7903 /* Buffer large enough to hold internal local symbols of any input
7905 Elf_Internal_Sym *internal_syms;
7906 /* Array large enough to hold a symbol index for each local symbol
7907 of any input BFD. */
7909 /* Array large enough to hold a section pointer for each local
7910 symbol of any input BFD. */
7911 asection **sections;
7912 /* Buffer for SHT_SYMTAB_SHNDX section. */
7913 Elf_External_Sym_Shndx *symshndxbuf;
7914 /* Number of STT_FILE syms seen. */
7915 size_t filesym_count;
7918 /* This struct is used to pass information to elf_link_output_extsym. */
7920 struct elf_outext_info
7923 bfd_boolean localsyms;
7924 bfd_boolean file_sym_done;
7925 struct elf_final_link_info *flinfo;
7929 /* Support for evaluating a complex relocation.
7931 Complex relocations are generalized, self-describing relocations. The
7932 implementation of them consists of two parts: complex symbols, and the
7933 relocations themselves.
7935 The relocations are use a reserved elf-wide relocation type code (R_RELC
7936 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7937 information (start bit, end bit, word width, etc) into the addend. This
7938 information is extracted from CGEN-generated operand tables within gas.
7940 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7941 internal) representing prefix-notation expressions, including but not
7942 limited to those sorts of expressions normally encoded as addends in the
7943 addend field. The symbol mangling format is:
7946 | <unary-operator> ':' <node>
7947 | <binary-operator> ':' <node> ':' <node>
7950 <literal> := 's' <digits=N> ':' <N character symbol name>
7951 | 'S' <digits=N> ':' <N character section name>
7955 <binary-operator> := as in C
7956 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7959 set_symbol_value (bfd *bfd_with_globals,
7960 Elf_Internal_Sym *isymbuf,
7965 struct elf_link_hash_entry **sym_hashes;
7966 struct elf_link_hash_entry *h;
7967 size_t extsymoff = locsymcount;
7969 if (symidx < locsymcount)
7971 Elf_Internal_Sym *sym;
7973 sym = isymbuf + symidx;
7974 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7976 /* It is a local symbol: move it to the
7977 "absolute" section and give it a value. */
7978 sym->st_shndx = SHN_ABS;
7979 sym->st_value = val;
7982 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7986 /* It is a global symbol: set its link type
7987 to "defined" and give it a value. */
7989 sym_hashes = elf_sym_hashes (bfd_with_globals);
7990 h = sym_hashes [symidx - extsymoff];
7991 while (h->root.type == bfd_link_hash_indirect
7992 || h->root.type == bfd_link_hash_warning)
7993 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7994 h->root.type = bfd_link_hash_defined;
7995 h->root.u.def.value = val;
7996 h->root.u.def.section = bfd_abs_section_ptr;
8000 resolve_symbol (const char *name,
8002 struct elf_final_link_info *flinfo,
8004 Elf_Internal_Sym *isymbuf,
8007 Elf_Internal_Sym *sym;
8008 struct bfd_link_hash_entry *global_entry;
8009 const char *candidate = NULL;
8010 Elf_Internal_Shdr *symtab_hdr;
8013 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8015 for (i = 0; i < locsymcount; ++ i)
8019 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8022 candidate = bfd_elf_string_from_elf_section (input_bfd,
8023 symtab_hdr->sh_link,
8026 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8027 name, candidate, (unsigned long) sym->st_value);
8029 if (candidate && strcmp (candidate, name) == 0)
8031 asection *sec = flinfo->sections [i];
8033 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8034 *result += sec->output_offset + sec->output_section->vma;
8036 printf ("Found symbol with value %8.8lx\n",
8037 (unsigned long) *result);
8043 /* Hmm, haven't found it yet. perhaps it is a global. */
8044 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8045 FALSE, FALSE, TRUE);
8049 if (global_entry->type == bfd_link_hash_defined
8050 || global_entry->type == bfd_link_hash_defweak)
8052 *result = (global_entry->u.def.value
8053 + global_entry->u.def.section->output_section->vma
8054 + global_entry->u.def.section->output_offset);
8056 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8057 global_entry->root.string, (unsigned long) *result);
8065 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8066 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8067 names like "foo.end" which is the end address of section "foo". */
8070 resolve_section (const char *name,
8078 for (curr = sections; curr; curr = curr->next)
8079 if (strcmp (curr->name, name) == 0)
8081 *result = curr->vma;
8085 /* Hmm. still haven't found it. try pseudo-section names. */
8086 /* FIXME: This could be coded more efficiently... */
8087 for (curr = sections; curr; curr = curr->next)
8089 len = strlen (curr->name);
8090 if (len > strlen (name))
8093 if (strncmp (curr->name, name, len) == 0)
8095 if (strncmp (".end", name + len, 4) == 0)
8097 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8101 /* Insert more pseudo-section names here, if you like. */
8109 undefined_reference (const char *reftype, const char *name)
8111 /* xgettext:c-format */
8112 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8117 eval_symbol (bfd_vma *result,
8120 struct elf_final_link_info *flinfo,
8122 Elf_Internal_Sym *isymbuf,
8131 const char *sym = *symp;
8133 bfd_boolean symbol_is_section = FALSE;
8138 if (len < 1 || len > sizeof (symbuf))
8140 bfd_set_error (bfd_error_invalid_operation);
8153 *result = strtoul (sym, (char **) symp, 16);
8157 symbol_is_section = TRUE;
8161 symlen = strtol (sym, (char **) symp, 10);
8162 sym = *symp + 1; /* Skip the trailing ':'. */
8164 if (symend < sym || symlen + 1 > sizeof (symbuf))
8166 bfd_set_error (bfd_error_invalid_operation);
8170 memcpy (symbuf, sym, symlen);
8171 symbuf[symlen] = '\0';
8172 *symp = sym + symlen;
8174 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8175 the symbol as a section, or vice-versa. so we're pretty liberal in our
8176 interpretation here; section means "try section first", not "must be a
8177 section", and likewise with symbol. */
8179 if (symbol_is_section)
8181 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8182 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8183 isymbuf, locsymcount))
8185 undefined_reference ("section", symbuf);
8191 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8192 isymbuf, locsymcount)
8193 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8196 undefined_reference ("symbol", symbuf);
8203 /* All that remains are operators. */
8205 #define UNARY_OP(op) \
8206 if (strncmp (sym, #op, strlen (#op)) == 0) \
8208 sym += strlen (#op); \
8212 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8213 isymbuf, locsymcount, signed_p)) \
8216 *result = op ((bfd_signed_vma) a); \
8222 #define BINARY_OP(op) \
8223 if (strncmp (sym, #op, strlen (#op)) == 0) \
8225 sym += strlen (#op); \
8229 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8230 isymbuf, locsymcount, signed_p)) \
8233 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8234 isymbuf, locsymcount, signed_p)) \
8237 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8267 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8268 bfd_set_error (bfd_error_invalid_operation);
8274 put_value (bfd_vma size,
8275 unsigned long chunksz,
8280 location += (size - chunksz);
8282 for (; size; size -= chunksz, location -= chunksz)
8287 bfd_put_8 (input_bfd, x, location);
8291 bfd_put_16 (input_bfd, x, location);
8295 bfd_put_32 (input_bfd, x, location);
8296 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8302 bfd_put_64 (input_bfd, x, location);
8303 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8316 get_value (bfd_vma size,
8317 unsigned long chunksz,
8324 /* Sanity checks. */
8325 BFD_ASSERT (chunksz <= sizeof (x)
8328 && (size % chunksz) == 0
8329 && input_bfd != NULL
8330 && location != NULL);
8332 if (chunksz == sizeof (x))
8334 BFD_ASSERT (size == chunksz);
8336 /* Make sure that we do not perform an undefined shift operation.
8337 We know that size == chunksz so there will only be one iteration
8338 of the loop below. */
8342 shift = 8 * chunksz;
8344 for (; size; size -= chunksz, location += chunksz)
8349 x = (x << shift) | bfd_get_8 (input_bfd, location);
8352 x = (x << shift) | bfd_get_16 (input_bfd, location);
8355 x = (x << shift) | bfd_get_32 (input_bfd, location);
8359 x = (x << shift) | bfd_get_64 (input_bfd, location);
8370 decode_complex_addend (unsigned long *start, /* in bits */
8371 unsigned long *oplen, /* in bits */
8372 unsigned long *len, /* in bits */
8373 unsigned long *wordsz, /* in bytes */
8374 unsigned long *chunksz, /* in bytes */
8375 unsigned long *lsb0_p,
8376 unsigned long *signed_p,
8377 unsigned long *trunc_p,
8378 unsigned long encoded)
8380 * start = encoded & 0x3F;
8381 * len = (encoded >> 6) & 0x3F;
8382 * oplen = (encoded >> 12) & 0x3F;
8383 * wordsz = (encoded >> 18) & 0xF;
8384 * chunksz = (encoded >> 22) & 0xF;
8385 * lsb0_p = (encoded >> 27) & 1;
8386 * signed_p = (encoded >> 28) & 1;
8387 * trunc_p = (encoded >> 29) & 1;
8390 bfd_reloc_status_type
8391 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8392 asection *input_section ATTRIBUTE_UNUSED,
8394 Elf_Internal_Rela *rel,
8397 bfd_vma shift, x, mask;
8398 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8399 bfd_reloc_status_type r;
8401 /* Perform this reloc, since it is complex.
8402 (this is not to say that it necessarily refers to a complex
8403 symbol; merely that it is a self-describing CGEN based reloc.
8404 i.e. the addend has the complete reloc information (bit start, end,
8405 word size, etc) encoded within it.). */
8407 decode_complex_addend (&start, &oplen, &len, &wordsz,
8408 &chunksz, &lsb0_p, &signed_p,
8409 &trunc_p, rel->r_addend);
8411 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8414 shift = (start + 1) - len;
8416 shift = (8 * wordsz) - (start + len);
8418 x = get_value (wordsz, chunksz, input_bfd,
8419 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8422 printf ("Doing complex reloc: "
8423 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8424 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8425 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8426 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8427 oplen, (unsigned long) x, (unsigned long) mask,
8428 (unsigned long) relocation);
8433 /* Now do an overflow check. */
8434 r = bfd_check_overflow ((signed_p
8435 ? complain_overflow_signed
8436 : complain_overflow_unsigned),
8437 len, 0, (8 * wordsz),
8441 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8444 printf (" relocation: %8.8lx\n"
8445 " shifted mask: %8.8lx\n"
8446 " shifted/masked reloc: %8.8lx\n"
8447 " result: %8.8lx\n",
8448 (unsigned long) relocation, (unsigned long) (mask << shift),
8449 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8451 put_value (wordsz, chunksz, input_bfd, x,
8452 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8456 /* Functions to read r_offset from external (target order) reloc
8457 entry. Faster than bfd_getl32 et al, because we let the compiler
8458 know the value is aligned. */
8461 ext32l_r_offset (const void *p)
8468 const union aligned32 *a
8469 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8471 uint32_t aval = ( (uint32_t) a->c[0]
8472 | (uint32_t) a->c[1] << 8
8473 | (uint32_t) a->c[2] << 16
8474 | (uint32_t) a->c[3] << 24);
8479 ext32b_r_offset (const void *p)
8486 const union aligned32 *a
8487 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8489 uint32_t aval = ( (uint32_t) a->c[0] << 24
8490 | (uint32_t) a->c[1] << 16
8491 | (uint32_t) a->c[2] << 8
8492 | (uint32_t) a->c[3]);
8496 #ifdef BFD_HOST_64_BIT
8498 ext64l_r_offset (const void *p)
8505 const union aligned64 *a
8506 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8508 uint64_t aval = ( (uint64_t) a->c[0]
8509 | (uint64_t) a->c[1] << 8
8510 | (uint64_t) a->c[2] << 16
8511 | (uint64_t) a->c[3] << 24
8512 | (uint64_t) a->c[4] << 32
8513 | (uint64_t) a->c[5] << 40
8514 | (uint64_t) a->c[6] << 48
8515 | (uint64_t) a->c[7] << 56);
8520 ext64b_r_offset (const void *p)
8527 const union aligned64 *a
8528 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8530 uint64_t aval = ( (uint64_t) a->c[0] << 56
8531 | (uint64_t) a->c[1] << 48
8532 | (uint64_t) a->c[2] << 40
8533 | (uint64_t) a->c[3] << 32
8534 | (uint64_t) a->c[4] << 24
8535 | (uint64_t) a->c[5] << 16
8536 | (uint64_t) a->c[6] << 8
8537 | (uint64_t) a->c[7]);
8542 /* When performing a relocatable link, the input relocations are
8543 preserved. But, if they reference global symbols, the indices
8544 referenced must be updated. Update all the relocations found in
8548 elf_link_adjust_relocs (bfd *abfd,
8550 struct bfd_elf_section_reloc_data *reldata,
8554 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8556 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8557 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8558 bfd_vma r_type_mask;
8560 unsigned int count = reldata->count;
8561 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8563 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8565 swap_in = bed->s->swap_reloc_in;
8566 swap_out = bed->s->swap_reloc_out;
8568 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8570 swap_in = bed->s->swap_reloca_in;
8571 swap_out = bed->s->swap_reloca_out;
8576 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8579 if (bed->s->arch_size == 32)
8586 r_type_mask = 0xffffffff;
8590 erela = reldata->hdr->contents;
8591 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8593 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8596 if (*rel_hash == NULL)
8599 BFD_ASSERT ((*rel_hash)->indx >= 0);
8601 (*swap_in) (abfd, erela, irela);
8602 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8603 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8604 | (irela[j].r_info & r_type_mask));
8605 (*swap_out) (abfd, irela, erela);
8608 if (bed->elf_backend_update_relocs)
8609 (*bed->elf_backend_update_relocs) (sec, reldata);
8611 if (sort && count != 0)
8613 bfd_vma (*ext_r_off) (const void *);
8616 bfd_byte *base, *end, *p, *loc;
8617 bfd_byte *buf = NULL;
8619 if (bed->s->arch_size == 32)
8621 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8622 ext_r_off = ext32l_r_offset;
8623 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8624 ext_r_off = ext32b_r_offset;
8630 #ifdef BFD_HOST_64_BIT
8631 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8632 ext_r_off = ext64l_r_offset;
8633 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8634 ext_r_off = ext64b_r_offset;
8640 /* Must use a stable sort here. A modified insertion sort,
8641 since the relocs are mostly sorted already. */
8642 elt_size = reldata->hdr->sh_entsize;
8643 base = reldata->hdr->contents;
8644 end = base + count * elt_size;
8645 if (elt_size > sizeof (Elf64_External_Rela))
8648 /* Ensure the first element is lowest. This acts as a sentinel,
8649 speeding the main loop below. */
8650 r_off = (*ext_r_off) (base);
8651 for (p = loc = base; (p += elt_size) < end; )
8653 bfd_vma r_off2 = (*ext_r_off) (p);
8662 /* Don't just swap *base and *loc as that changes the order
8663 of the original base[0] and base[1] if they happen to
8664 have the same r_offset. */
8665 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8666 memcpy (onebuf, loc, elt_size);
8667 memmove (base + elt_size, base, loc - base);
8668 memcpy (base, onebuf, elt_size);
8671 for (p = base + elt_size; (p += elt_size) < end; )
8673 /* base to p is sorted, *p is next to insert. */
8674 r_off = (*ext_r_off) (p);
8675 /* Search the sorted region for location to insert. */
8677 while (r_off < (*ext_r_off) (loc))
8682 /* Chances are there is a run of relocs to insert here,
8683 from one of more input files. Files are not always
8684 linked in order due to the way elf_link_input_bfd is
8685 called. See pr17666. */
8686 size_t sortlen = p - loc;
8687 bfd_vma r_off2 = (*ext_r_off) (loc);
8688 size_t runlen = elt_size;
8689 size_t buf_size = 96 * 1024;
8690 while (p + runlen < end
8691 && (sortlen <= buf_size
8692 || runlen + elt_size <= buf_size)
8693 && r_off2 > (*ext_r_off) (p + runlen))
8697 buf = bfd_malloc (buf_size);
8701 if (runlen < sortlen)
8703 memcpy (buf, p, runlen);
8704 memmove (loc + runlen, loc, sortlen);
8705 memcpy (loc, buf, runlen);
8709 memcpy (buf, loc, sortlen);
8710 memmove (loc, p, runlen);
8711 memcpy (loc + runlen, buf, sortlen);
8713 p += runlen - elt_size;
8716 /* Hashes are no longer valid. */
8717 free (reldata->hashes);
8718 reldata->hashes = NULL;
8724 struct elf_link_sort_rela
8730 enum elf_reloc_type_class type;
8731 /* We use this as an array of size int_rels_per_ext_rel. */
8732 Elf_Internal_Rela rela[1];
8736 elf_link_sort_cmp1 (const void *A, const void *B)
8738 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8739 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8740 int relativea, relativeb;
8742 relativea = a->type == reloc_class_relative;
8743 relativeb = b->type == reloc_class_relative;
8745 if (relativea < relativeb)
8747 if (relativea > relativeb)
8749 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8751 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8753 if (a->rela->r_offset < b->rela->r_offset)
8755 if (a->rela->r_offset > b->rela->r_offset)
8761 elf_link_sort_cmp2 (const void *A, const void *B)
8763 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8764 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8766 if (a->type < b->type)
8768 if (a->type > b->type)
8770 if (a->u.offset < b->u.offset)
8772 if (a->u.offset > b->u.offset)
8774 if (a->rela->r_offset < b->rela->r_offset)
8776 if (a->rela->r_offset > b->rela->r_offset)
8782 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8784 asection *dynamic_relocs;
8787 bfd_size_type count, size;
8788 size_t i, ret, sort_elt, ext_size;
8789 bfd_byte *sort, *s_non_relative, *p;
8790 struct elf_link_sort_rela *sq;
8791 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8792 int i2e = bed->s->int_rels_per_ext_rel;
8793 unsigned int opb = bfd_octets_per_byte (abfd);
8794 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8795 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8796 struct bfd_link_order *lo;
8798 bfd_boolean use_rela;
8800 /* Find a dynamic reloc section. */
8801 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8802 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8803 if (rela_dyn != NULL && rela_dyn->size > 0
8804 && rel_dyn != NULL && rel_dyn->size > 0)
8806 bfd_boolean use_rela_initialised = FALSE;
8808 /* This is just here to stop gcc from complaining.
8809 Its initialization checking code is not perfect. */
8812 /* Both sections are present. Examine the sizes
8813 of the indirect sections to help us choose. */
8814 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8815 if (lo->type == bfd_indirect_link_order)
8817 asection *o = lo->u.indirect.section;
8819 if ((o->size % bed->s->sizeof_rela) == 0)
8821 if ((o->size % bed->s->sizeof_rel) == 0)
8822 /* Section size is divisible by both rel and rela sizes.
8823 It is of no help to us. */
8827 /* Section size is only divisible by rela. */
8828 if (use_rela_initialised && (use_rela == FALSE))
8830 _bfd_error_handler (_("%B: Unable to sort relocs - "
8831 "they are in more than one size"),
8833 bfd_set_error (bfd_error_invalid_operation);
8839 use_rela_initialised = TRUE;
8843 else if ((o->size % bed->s->sizeof_rel) == 0)
8845 /* Section size is only divisible by rel. */
8846 if (use_rela_initialised && (use_rela == TRUE))
8848 _bfd_error_handler (_("%B: Unable to sort relocs - "
8849 "they are in more than one size"),
8851 bfd_set_error (bfd_error_invalid_operation);
8857 use_rela_initialised = TRUE;
8862 /* The section size is not divisible by either -
8863 something is wrong. */
8864 _bfd_error_handler (_("%B: Unable to sort relocs - "
8865 "they are of an unknown size"), abfd);
8866 bfd_set_error (bfd_error_invalid_operation);
8871 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8872 if (lo->type == bfd_indirect_link_order)
8874 asection *o = lo->u.indirect.section;
8876 if ((o->size % bed->s->sizeof_rela) == 0)
8878 if ((o->size % bed->s->sizeof_rel) == 0)
8879 /* Section size is divisible by both rel and rela sizes.
8880 It is of no help to us. */
8884 /* Section size is only divisible by rela. */
8885 if (use_rela_initialised && (use_rela == FALSE))
8887 _bfd_error_handler (_("%B: Unable to sort relocs - "
8888 "they are in more than one size"),
8890 bfd_set_error (bfd_error_invalid_operation);
8896 use_rela_initialised = TRUE;
8900 else if ((o->size % bed->s->sizeof_rel) == 0)
8902 /* Section size is only divisible by rel. */
8903 if (use_rela_initialised && (use_rela == TRUE))
8905 _bfd_error_handler (_("%B: Unable to sort relocs - "
8906 "they are in more than one size"),
8908 bfd_set_error (bfd_error_invalid_operation);
8914 use_rela_initialised = TRUE;
8919 /* The section size is not divisible by either -
8920 something is wrong. */
8921 _bfd_error_handler (_("%B: Unable to sort relocs - "
8922 "they are of an unknown size"), abfd);
8923 bfd_set_error (bfd_error_invalid_operation);
8928 if (! use_rela_initialised)
8932 else if (rela_dyn != NULL && rela_dyn->size > 0)
8934 else if (rel_dyn != NULL && rel_dyn->size > 0)
8941 dynamic_relocs = rela_dyn;
8942 ext_size = bed->s->sizeof_rela;
8943 swap_in = bed->s->swap_reloca_in;
8944 swap_out = bed->s->swap_reloca_out;
8948 dynamic_relocs = rel_dyn;
8949 ext_size = bed->s->sizeof_rel;
8950 swap_in = bed->s->swap_reloc_in;
8951 swap_out = bed->s->swap_reloc_out;
8955 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8956 if (lo->type == bfd_indirect_link_order)
8957 size += lo->u.indirect.section->size;
8959 if (size != dynamic_relocs->size)
8962 sort_elt = (sizeof (struct elf_link_sort_rela)
8963 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8965 count = dynamic_relocs->size / ext_size;
8968 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8972 (*info->callbacks->warning)
8973 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8977 if (bed->s->arch_size == 32)
8978 r_sym_mask = ~(bfd_vma) 0xff;
8980 r_sym_mask = ~(bfd_vma) 0xffffffff;
8982 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8983 if (lo->type == bfd_indirect_link_order)
8985 bfd_byte *erel, *erelend;
8986 asection *o = lo->u.indirect.section;
8988 if (o->contents == NULL && o->size != 0)
8990 /* This is a reloc section that is being handled as a normal
8991 section. See bfd_section_from_shdr. We can't combine
8992 relocs in this case. */
8997 erelend = o->contents + o->size;
8998 p = sort + o->output_offset * opb / ext_size * sort_elt;
9000 while (erel < erelend)
9002 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9004 (*swap_in) (abfd, erel, s->rela);
9005 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9006 s->u.sym_mask = r_sym_mask;
9012 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9014 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9016 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9017 if (s->type != reloc_class_relative)
9023 sq = (struct elf_link_sort_rela *) s_non_relative;
9024 for (; i < count; i++, p += sort_elt)
9026 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9027 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9029 sp->u.offset = sq->rela->r_offset;
9032 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9034 struct elf_link_hash_table *htab = elf_hash_table (info);
9035 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9037 /* We have plt relocs in .rela.dyn. */
9038 sq = (struct elf_link_sort_rela *) sort;
9039 for (i = 0; i < count; i++)
9040 if (sq[count - i - 1].type != reloc_class_plt)
9042 if (i != 0 && htab->srelplt->size == i * ext_size)
9044 struct bfd_link_order **plo;
9045 /* Put srelplt link_order last. This is so the output_offset
9046 set in the next loop is correct for DT_JMPREL. */
9047 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9048 if ((*plo)->type == bfd_indirect_link_order
9049 && (*plo)->u.indirect.section == htab->srelplt)
9055 plo = &(*plo)->next;
9058 dynamic_relocs->map_tail.link_order = lo;
9063 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9064 if (lo->type == bfd_indirect_link_order)
9066 bfd_byte *erel, *erelend;
9067 asection *o = lo->u.indirect.section;
9070 erelend = o->contents + o->size;
9071 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9072 while (erel < erelend)
9074 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9075 (*swap_out) (abfd, s->rela, erel);
9082 *psec = dynamic_relocs;
9086 /* Add a symbol to the output symbol string table. */
9089 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9091 Elf_Internal_Sym *elfsym,
9092 asection *input_sec,
9093 struct elf_link_hash_entry *h)
9095 int (*output_symbol_hook)
9096 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9097 struct elf_link_hash_entry *);
9098 struct elf_link_hash_table *hash_table;
9099 const struct elf_backend_data *bed;
9100 bfd_size_type strtabsize;
9102 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9104 bed = get_elf_backend_data (flinfo->output_bfd);
9105 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9106 if (output_symbol_hook != NULL)
9108 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9115 || (input_sec->flags & SEC_EXCLUDE))
9116 elfsym->st_name = (unsigned long) -1;
9119 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9120 to get the final offset for st_name. */
9122 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9124 if (elfsym->st_name == (unsigned long) -1)
9128 hash_table = elf_hash_table (flinfo->info);
9129 strtabsize = hash_table->strtabsize;
9130 if (strtabsize <= hash_table->strtabcount)
9132 strtabsize += strtabsize;
9133 hash_table->strtabsize = strtabsize;
9134 strtabsize *= sizeof (*hash_table->strtab);
9136 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9138 if (hash_table->strtab == NULL)
9141 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9142 hash_table->strtab[hash_table->strtabcount].dest_index
9143 = hash_table->strtabcount;
9144 hash_table->strtab[hash_table->strtabcount].destshndx_index
9145 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9147 bfd_get_symcount (flinfo->output_bfd) += 1;
9148 hash_table->strtabcount += 1;
9153 /* Swap symbols out to the symbol table and flush the output symbols to
9157 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9159 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9162 const struct elf_backend_data *bed;
9164 Elf_Internal_Shdr *hdr;
9168 if (!hash_table->strtabcount)
9171 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9173 bed = get_elf_backend_data (flinfo->output_bfd);
9175 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9176 symbuf = (bfd_byte *) bfd_malloc (amt);
9180 if (flinfo->symshndxbuf)
9182 amt = sizeof (Elf_External_Sym_Shndx);
9183 amt *= bfd_get_symcount (flinfo->output_bfd);
9184 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9185 if (flinfo->symshndxbuf == NULL)
9192 for (i = 0; i < hash_table->strtabcount; i++)
9194 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9195 if (elfsym->sym.st_name == (unsigned long) -1)
9196 elfsym->sym.st_name = 0;
9199 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9200 elfsym->sym.st_name);
9201 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9202 ((bfd_byte *) symbuf
9203 + (elfsym->dest_index
9204 * bed->s->sizeof_sym)),
9205 (flinfo->symshndxbuf
9206 + elfsym->destshndx_index));
9209 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9210 pos = hdr->sh_offset + hdr->sh_size;
9211 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9212 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9213 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9215 hdr->sh_size += amt;
9223 free (hash_table->strtab);
9224 hash_table->strtab = NULL;
9229 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9232 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9234 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9235 && sym->st_shndx < SHN_LORESERVE)
9237 /* The gABI doesn't support dynamic symbols in output sections
9240 /* xgettext:c-format */
9241 (_("%B: Too many sections: %d (>= %d)"),
9242 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9243 bfd_set_error (bfd_error_nonrepresentable_section);
9249 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9250 allowing an unsatisfied unversioned symbol in the DSO to match a
9251 versioned symbol that would normally require an explicit version.
9252 We also handle the case that a DSO references a hidden symbol
9253 which may be satisfied by a versioned symbol in another DSO. */
9256 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9257 const struct elf_backend_data *bed,
9258 struct elf_link_hash_entry *h)
9261 struct elf_link_loaded_list *loaded;
9263 if (!is_elf_hash_table (info->hash))
9266 /* Check indirect symbol. */
9267 while (h->root.type == bfd_link_hash_indirect)
9268 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9270 switch (h->root.type)
9276 case bfd_link_hash_undefined:
9277 case bfd_link_hash_undefweak:
9278 abfd = h->root.u.undef.abfd;
9280 || (abfd->flags & DYNAMIC) == 0
9281 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9285 case bfd_link_hash_defined:
9286 case bfd_link_hash_defweak:
9287 abfd = h->root.u.def.section->owner;
9290 case bfd_link_hash_common:
9291 abfd = h->root.u.c.p->section->owner;
9294 BFD_ASSERT (abfd != NULL);
9296 for (loaded = elf_hash_table (info)->loaded;
9298 loaded = loaded->next)
9301 Elf_Internal_Shdr *hdr;
9305 Elf_Internal_Shdr *versymhdr;
9306 Elf_Internal_Sym *isym;
9307 Elf_Internal_Sym *isymend;
9308 Elf_Internal_Sym *isymbuf;
9309 Elf_External_Versym *ever;
9310 Elf_External_Versym *extversym;
9312 input = loaded->abfd;
9314 /* We check each DSO for a possible hidden versioned definition. */
9316 || (input->flags & DYNAMIC) == 0
9317 || elf_dynversym (input) == 0)
9320 hdr = &elf_tdata (input)->dynsymtab_hdr;
9322 symcount = hdr->sh_size / bed->s->sizeof_sym;
9323 if (elf_bad_symtab (input))
9325 extsymcount = symcount;
9330 extsymcount = symcount - hdr->sh_info;
9331 extsymoff = hdr->sh_info;
9334 if (extsymcount == 0)
9337 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9339 if (isymbuf == NULL)
9342 /* Read in any version definitions. */
9343 versymhdr = &elf_tdata (input)->dynversym_hdr;
9344 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9345 if (extversym == NULL)
9348 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9349 || (bfd_bread (extversym, versymhdr->sh_size, input)
9350 != versymhdr->sh_size))
9358 ever = extversym + extsymoff;
9359 isymend = isymbuf + extsymcount;
9360 for (isym = isymbuf; isym < isymend; isym++, ever++)
9363 Elf_Internal_Versym iver;
9364 unsigned short version_index;
9366 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9367 || isym->st_shndx == SHN_UNDEF)
9370 name = bfd_elf_string_from_elf_section (input,
9373 if (strcmp (name, h->root.root.string) != 0)
9376 _bfd_elf_swap_versym_in (input, ever, &iver);
9378 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9380 && h->forced_local))
9382 /* If we have a non-hidden versioned sym, then it should
9383 have provided a definition for the undefined sym unless
9384 it is defined in a non-shared object and forced local.
9389 version_index = iver.vs_vers & VERSYM_VERSION;
9390 if (version_index == 1 || version_index == 2)
9392 /* This is the base or first version. We can use it. */
9406 /* Convert ELF common symbol TYPE. */
9409 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9411 /* Commom symbol can only appear in relocatable link. */
9412 if (!bfd_link_relocatable (info))
9414 switch (info->elf_stt_common)
9418 case elf_stt_common:
9421 case no_elf_stt_common:
9428 /* Add an external symbol to the symbol table. This is called from
9429 the hash table traversal routine. When generating a shared object,
9430 we go through the symbol table twice. The first time we output
9431 anything that might have been forced to local scope in a version
9432 script. The second time we output the symbols that are still
9436 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9438 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9439 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9440 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9442 Elf_Internal_Sym sym;
9443 asection *input_sec;
9444 const struct elf_backend_data *bed;
9449 if (h->root.type == bfd_link_hash_warning)
9451 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9452 if (h->root.type == bfd_link_hash_new)
9456 /* Decide whether to output this symbol in this pass. */
9457 if (eoinfo->localsyms)
9459 if (!h->forced_local)
9464 if (h->forced_local)
9468 bed = get_elf_backend_data (flinfo->output_bfd);
9470 if (h->root.type == bfd_link_hash_undefined)
9472 /* If we have an undefined symbol reference here then it must have
9473 come from a shared library that is being linked in. (Undefined
9474 references in regular files have already been handled unless
9475 they are in unreferenced sections which are removed by garbage
9477 bfd_boolean ignore_undef = FALSE;
9479 /* Some symbols may be special in that the fact that they're
9480 undefined can be safely ignored - let backend determine that. */
9481 if (bed->elf_backend_ignore_undef_symbol)
9482 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9484 /* If we are reporting errors for this situation then do so now. */
9487 && (!h->ref_regular || flinfo->info->gc_sections)
9488 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9489 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9490 (*flinfo->info->callbacks->undefined_symbol)
9491 (flinfo->info, h->root.root.string,
9492 h->ref_regular ? NULL : h->root.u.undef.abfd,
9494 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9496 /* Strip a global symbol defined in a discarded section. */
9501 /* We should also warn if a forced local symbol is referenced from
9502 shared libraries. */
9503 if (bfd_link_executable (flinfo->info)
9508 && h->ref_dynamic_nonweak
9509 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9513 struct elf_link_hash_entry *hi = h;
9515 /* Check indirect symbol. */
9516 while (hi->root.type == bfd_link_hash_indirect)
9517 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9519 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9520 /* xgettext:c-format */
9521 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9522 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9523 /* xgettext:c-format */
9524 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9526 /* xgettext:c-format */
9527 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9528 def_bfd = flinfo->output_bfd;
9529 if (hi->root.u.def.section != bfd_abs_section_ptr)
9530 def_bfd = hi->root.u.def.section->owner;
9531 _bfd_error_handler (msg, flinfo->output_bfd,
9532 h->root.root.string, def_bfd);
9533 bfd_set_error (bfd_error_bad_value);
9534 eoinfo->failed = TRUE;
9538 /* We don't want to output symbols that have never been mentioned by
9539 a regular file, or that we have been told to strip. However, if
9540 h->indx is set to -2, the symbol is used by a reloc and we must
9545 else if ((h->def_dynamic
9547 || h->root.type == bfd_link_hash_new)
9551 else if (flinfo->info->strip == strip_all)
9553 else if (flinfo->info->strip == strip_some
9554 && bfd_hash_lookup (flinfo->info->keep_hash,
9555 h->root.root.string, FALSE, FALSE) == NULL)
9557 else if ((h->root.type == bfd_link_hash_defined
9558 || h->root.type == bfd_link_hash_defweak)
9559 && ((flinfo->info->strip_discarded
9560 && discarded_section (h->root.u.def.section))
9561 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9562 && h->root.u.def.section->owner != NULL
9563 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9565 else if ((h->root.type == bfd_link_hash_undefined
9566 || h->root.type == bfd_link_hash_undefweak)
9567 && h->root.u.undef.abfd != NULL
9568 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9573 /* If we're stripping it, and it's not a dynamic symbol, there's
9574 nothing else to do. However, if it is a forced local symbol or
9575 an ifunc symbol we need to give the backend finish_dynamic_symbol
9576 function a chance to make it dynamic. */
9579 && type != STT_GNU_IFUNC
9580 && !h->forced_local)
9584 sym.st_size = h->size;
9585 sym.st_other = h->other;
9586 switch (h->root.type)
9589 case bfd_link_hash_new:
9590 case bfd_link_hash_warning:
9594 case bfd_link_hash_undefined:
9595 case bfd_link_hash_undefweak:
9596 input_sec = bfd_und_section_ptr;
9597 sym.st_shndx = SHN_UNDEF;
9600 case bfd_link_hash_defined:
9601 case bfd_link_hash_defweak:
9603 input_sec = h->root.u.def.section;
9604 if (input_sec->output_section != NULL)
9607 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9608 input_sec->output_section);
9609 if (sym.st_shndx == SHN_BAD)
9612 /* xgettext:c-format */
9613 (_("%B: could not find output section %A for input section %A"),
9614 flinfo->output_bfd, input_sec->output_section, input_sec);
9615 bfd_set_error (bfd_error_nonrepresentable_section);
9616 eoinfo->failed = TRUE;
9620 /* ELF symbols in relocatable files are section relative,
9621 but in nonrelocatable files they are virtual
9623 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9624 if (!bfd_link_relocatable (flinfo->info))
9626 sym.st_value += input_sec->output_section->vma;
9627 if (h->type == STT_TLS)
9629 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9630 if (tls_sec != NULL)
9631 sym.st_value -= tls_sec->vma;
9637 BFD_ASSERT (input_sec->owner == NULL
9638 || (input_sec->owner->flags & DYNAMIC) != 0);
9639 sym.st_shndx = SHN_UNDEF;
9640 input_sec = bfd_und_section_ptr;
9645 case bfd_link_hash_common:
9646 input_sec = h->root.u.c.p->section;
9647 sym.st_shndx = bed->common_section_index (input_sec);
9648 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9651 case bfd_link_hash_indirect:
9652 /* These symbols are created by symbol versioning. They point
9653 to the decorated version of the name. For example, if the
9654 symbol foo@@GNU_1.2 is the default, which should be used when
9655 foo is used with no version, then we add an indirect symbol
9656 foo which points to foo@@GNU_1.2. We ignore these symbols,
9657 since the indirected symbol is already in the hash table. */
9661 if (type == STT_COMMON || type == STT_OBJECT)
9662 switch (h->root.type)
9664 case bfd_link_hash_common:
9665 type = elf_link_convert_common_type (flinfo->info, type);
9667 case bfd_link_hash_defined:
9668 case bfd_link_hash_defweak:
9669 if (bed->common_definition (&sym))
9670 type = elf_link_convert_common_type (flinfo->info, type);
9674 case bfd_link_hash_undefined:
9675 case bfd_link_hash_undefweak:
9681 if (h->forced_local)
9683 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9684 /* Turn off visibility on local symbol. */
9685 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9687 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9688 else if (h->unique_global && h->def_regular)
9689 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9690 else if (h->root.type == bfd_link_hash_undefweak
9691 || h->root.type == bfd_link_hash_defweak)
9692 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9694 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9695 sym.st_target_internal = h->target_internal;
9697 /* Give the processor backend a chance to tweak the symbol value,
9698 and also to finish up anything that needs to be done for this
9699 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9700 forced local syms when non-shared is due to a historical quirk.
9701 STT_GNU_IFUNC symbol must go through PLT. */
9702 if ((h->type == STT_GNU_IFUNC
9704 && !bfd_link_relocatable (flinfo->info))
9705 || ((h->dynindx != -1
9707 && ((bfd_link_pic (flinfo->info)
9708 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9709 || h->root.type != bfd_link_hash_undefweak))
9710 || !h->forced_local)
9711 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9713 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9714 (flinfo->output_bfd, flinfo->info, h, &sym)))
9716 eoinfo->failed = TRUE;
9721 /* If we are marking the symbol as undefined, and there are no
9722 non-weak references to this symbol from a regular object, then
9723 mark the symbol as weak undefined; if there are non-weak
9724 references, mark the symbol as strong. We can't do this earlier,
9725 because it might not be marked as undefined until the
9726 finish_dynamic_symbol routine gets through with it. */
9727 if (sym.st_shndx == SHN_UNDEF
9729 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9730 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9733 type = ELF_ST_TYPE (sym.st_info);
9735 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9736 if (type == STT_GNU_IFUNC)
9739 if (h->ref_regular_nonweak)
9740 bindtype = STB_GLOBAL;
9742 bindtype = STB_WEAK;
9743 sym.st_info = ELF_ST_INFO (bindtype, type);
9746 /* If this is a symbol defined in a dynamic library, don't use the
9747 symbol size from the dynamic library. Relinking an executable
9748 against a new library may introduce gratuitous changes in the
9749 executable's symbols if we keep the size. */
9750 if (sym.st_shndx == SHN_UNDEF
9755 /* If a non-weak symbol with non-default visibility is not defined
9756 locally, it is a fatal error. */
9757 if (!bfd_link_relocatable (flinfo->info)
9758 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9759 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9760 && h->root.type == bfd_link_hash_undefined
9765 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9766 /* xgettext:c-format */
9767 msg = _("%B: protected symbol `%s' isn't defined");
9768 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9769 /* xgettext:c-format */
9770 msg = _("%B: internal symbol `%s' isn't defined");
9772 /* xgettext:c-format */
9773 msg = _("%B: hidden symbol `%s' isn't defined");
9774 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9775 bfd_set_error (bfd_error_bad_value);
9776 eoinfo->failed = TRUE;
9780 /* If this symbol should be put in the .dynsym section, then put it
9781 there now. We already know the symbol index. We also fill in
9782 the entry in the .hash section. */
9783 if (elf_hash_table (flinfo->info)->dynsym != NULL
9785 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9789 /* Since there is no version information in the dynamic string,
9790 if there is no version info in symbol version section, we will
9791 have a run-time problem if not linking executable, referenced
9792 by shared library, or not bound locally. */
9793 if (h->verinfo.verdef == NULL
9794 && (!bfd_link_executable (flinfo->info)
9796 || !h->def_regular))
9798 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9800 if (p && p [1] != '\0')
9803 /* xgettext:c-format */
9804 (_("%B: No symbol version section for versioned symbol `%s'"),
9805 flinfo->output_bfd, h->root.root.string);
9806 eoinfo->failed = TRUE;
9811 sym.st_name = h->dynstr_index;
9812 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9813 + h->dynindx * bed->s->sizeof_sym);
9814 if (!check_dynsym (flinfo->output_bfd, &sym))
9816 eoinfo->failed = TRUE;
9819 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9821 if (flinfo->hash_sec != NULL)
9823 size_t hash_entry_size;
9824 bfd_byte *bucketpos;
9829 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9830 bucket = h->u.elf_hash_value % bucketcount;
9833 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9834 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9835 + (bucket + 2) * hash_entry_size);
9836 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9837 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9839 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9840 ((bfd_byte *) flinfo->hash_sec->contents
9841 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9844 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9846 Elf_Internal_Versym iversym;
9847 Elf_External_Versym *eversym;
9849 if (!h->def_regular)
9851 if (h->verinfo.verdef == NULL
9852 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9853 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9854 iversym.vs_vers = 0;
9856 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9860 if (h->verinfo.vertree == NULL)
9861 iversym.vs_vers = 1;
9863 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9864 if (flinfo->info->create_default_symver)
9868 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9870 if (h->versioned == versioned_hidden && h->def_regular)
9871 iversym.vs_vers |= VERSYM_HIDDEN;
9873 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9874 eversym += h->dynindx;
9875 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9879 /* If the symbol is undefined, and we didn't output it to .dynsym,
9880 strip it from .symtab too. Obviously we can't do this for
9881 relocatable output or when needed for --emit-relocs. */
9882 else if (input_sec == bfd_und_section_ptr
9884 && !bfd_link_relocatable (flinfo->info))
9886 /* Also strip others that we couldn't earlier due to dynamic symbol
9890 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9893 /* Output a FILE symbol so that following locals are not associated
9894 with the wrong input file. We need one for forced local symbols
9895 if we've seen more than one FILE symbol or when we have exactly
9896 one FILE symbol but global symbols are present in a file other
9897 than the one with the FILE symbol. We also need one if linker
9898 defined symbols are present. In practice these conditions are
9899 always met, so just emit the FILE symbol unconditionally. */
9900 if (eoinfo->localsyms
9901 && !eoinfo->file_sym_done
9902 && eoinfo->flinfo->filesym_count != 0)
9904 Elf_Internal_Sym fsym;
9906 memset (&fsym, 0, sizeof (fsym));
9907 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9908 fsym.st_shndx = SHN_ABS;
9909 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9910 bfd_und_section_ptr, NULL))
9913 eoinfo->file_sym_done = TRUE;
9916 indx = bfd_get_symcount (flinfo->output_bfd);
9917 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9921 eoinfo->failed = TRUE;
9926 else if (h->indx == -2)
9932 /* Return TRUE if special handling is done for relocs in SEC against
9933 symbols defined in discarded sections. */
9936 elf_section_ignore_discarded_relocs (asection *sec)
9938 const struct elf_backend_data *bed;
9940 switch (sec->sec_info_type)
9942 case SEC_INFO_TYPE_STABS:
9943 case SEC_INFO_TYPE_EH_FRAME:
9944 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9950 bed = get_elf_backend_data (sec->owner);
9951 if (bed->elf_backend_ignore_discarded_relocs != NULL
9952 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9958 /* Return a mask saying how ld should treat relocations in SEC against
9959 symbols defined in discarded sections. If this function returns
9960 COMPLAIN set, ld will issue a warning message. If this function
9961 returns PRETEND set, and the discarded section was link-once and the
9962 same size as the kept link-once section, ld will pretend that the
9963 symbol was actually defined in the kept section. Otherwise ld will
9964 zero the reloc (at least that is the intent, but some cooperation by
9965 the target dependent code is needed, particularly for REL targets). */
9968 _bfd_elf_default_action_discarded (asection *sec)
9970 if (sec->flags & SEC_DEBUGGING)
9973 if (strcmp (".eh_frame", sec->name) == 0)
9976 if (strcmp (".gcc_except_table", sec->name) == 0)
9979 return COMPLAIN | PRETEND;
9982 /* Find a match between a section and a member of a section group. */
9985 match_group_member (asection *sec, asection *group,
9986 struct bfd_link_info *info)
9988 asection *first = elf_next_in_group (group);
9989 asection *s = first;
9993 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9996 s = elf_next_in_group (s);
10004 /* Check if the kept section of a discarded section SEC can be used
10005 to replace it. Return the replacement if it is OK. Otherwise return
10009 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10013 kept = sec->kept_section;
10016 if ((kept->flags & SEC_GROUP) != 0)
10017 kept = match_group_member (sec, kept, info);
10019 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10020 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10022 sec->kept_section = kept;
10027 /* Link an input file into the linker output file. This function
10028 handles all the sections and relocations of the input file at once.
10029 This is so that we only have to read the local symbols once, and
10030 don't have to keep them in memory. */
10033 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10035 int (*relocate_section)
10036 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10037 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10039 Elf_Internal_Shdr *symtab_hdr;
10040 size_t locsymcount;
10042 Elf_Internal_Sym *isymbuf;
10043 Elf_Internal_Sym *isym;
10044 Elf_Internal_Sym *isymend;
10046 asection **ppsection;
10048 const struct elf_backend_data *bed;
10049 struct elf_link_hash_entry **sym_hashes;
10050 bfd_size_type address_size;
10051 bfd_vma r_type_mask;
10053 bfd_boolean have_file_sym = FALSE;
10055 output_bfd = flinfo->output_bfd;
10056 bed = get_elf_backend_data (output_bfd);
10057 relocate_section = bed->elf_backend_relocate_section;
10059 /* If this is a dynamic object, we don't want to do anything here:
10060 we don't want the local symbols, and we don't want the section
10062 if ((input_bfd->flags & DYNAMIC) != 0)
10065 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10066 if (elf_bad_symtab (input_bfd))
10068 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10073 locsymcount = symtab_hdr->sh_info;
10074 extsymoff = symtab_hdr->sh_info;
10077 /* Read the local symbols. */
10078 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10079 if (isymbuf == NULL && locsymcount != 0)
10081 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10082 flinfo->internal_syms,
10083 flinfo->external_syms,
10084 flinfo->locsym_shndx);
10085 if (isymbuf == NULL)
10089 /* Find local symbol sections and adjust values of symbols in
10090 SEC_MERGE sections. Write out those local symbols we know are
10091 going into the output file. */
10092 isymend = isymbuf + locsymcount;
10093 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10095 isym++, pindex++, ppsection++)
10099 Elf_Internal_Sym osym;
10105 if (elf_bad_symtab (input_bfd))
10107 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10114 if (isym->st_shndx == SHN_UNDEF)
10115 isec = bfd_und_section_ptr;
10116 else if (isym->st_shndx == SHN_ABS)
10117 isec = bfd_abs_section_ptr;
10118 else if (isym->st_shndx == SHN_COMMON)
10119 isec = bfd_com_section_ptr;
10122 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10125 /* Don't attempt to output symbols with st_shnx in the
10126 reserved range other than SHN_ABS and SHN_COMMON. */
10130 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10131 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10133 _bfd_merged_section_offset (output_bfd, &isec,
10134 elf_section_data (isec)->sec_info,
10140 /* Don't output the first, undefined, symbol. In fact, don't
10141 output any undefined local symbol. */
10142 if (isec == bfd_und_section_ptr)
10145 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10147 /* We never output section symbols. Instead, we use the
10148 section symbol of the corresponding section in the output
10153 /* If we are stripping all symbols, we don't want to output this
10155 if (flinfo->info->strip == strip_all)
10158 /* If we are discarding all local symbols, we don't want to
10159 output this one. If we are generating a relocatable output
10160 file, then some of the local symbols may be required by
10161 relocs; we output them below as we discover that they are
10163 if (flinfo->info->discard == discard_all)
10166 /* If this symbol is defined in a section which we are
10167 discarding, we don't need to keep it. */
10168 if (isym->st_shndx != SHN_UNDEF
10169 && isym->st_shndx < SHN_LORESERVE
10170 && bfd_section_removed_from_list (output_bfd,
10171 isec->output_section))
10174 /* Get the name of the symbol. */
10175 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10180 /* See if we are discarding symbols with this name. */
10181 if ((flinfo->info->strip == strip_some
10182 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10184 || (((flinfo->info->discard == discard_sec_merge
10185 && (isec->flags & SEC_MERGE)
10186 && !bfd_link_relocatable (flinfo->info))
10187 || flinfo->info->discard == discard_l)
10188 && bfd_is_local_label_name (input_bfd, name)))
10191 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10193 if (input_bfd->lto_output)
10194 /* -flto puts a temp file name here. This means builds
10195 are not reproducible. Discard the symbol. */
10197 have_file_sym = TRUE;
10198 flinfo->filesym_count += 1;
10200 if (!have_file_sym)
10202 /* In the absence of debug info, bfd_find_nearest_line uses
10203 FILE symbols to determine the source file for local
10204 function symbols. Provide a FILE symbol here if input
10205 files lack such, so that their symbols won't be
10206 associated with a previous input file. It's not the
10207 source file, but the best we can do. */
10208 have_file_sym = TRUE;
10209 flinfo->filesym_count += 1;
10210 memset (&osym, 0, sizeof (osym));
10211 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10212 osym.st_shndx = SHN_ABS;
10213 if (!elf_link_output_symstrtab (flinfo,
10214 (input_bfd->lto_output ? NULL
10215 : input_bfd->filename),
10216 &osym, bfd_abs_section_ptr,
10223 /* Adjust the section index for the output file. */
10224 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10225 isec->output_section);
10226 if (osym.st_shndx == SHN_BAD)
10229 /* ELF symbols in relocatable files are section relative, but
10230 in executable files they are virtual addresses. Note that
10231 this code assumes that all ELF sections have an associated
10232 BFD section with a reasonable value for output_offset; below
10233 we assume that they also have a reasonable value for
10234 output_section. Any special sections must be set up to meet
10235 these requirements. */
10236 osym.st_value += isec->output_offset;
10237 if (!bfd_link_relocatable (flinfo->info))
10239 osym.st_value += isec->output_section->vma;
10240 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10242 /* STT_TLS symbols are relative to PT_TLS segment base. */
10243 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10244 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10248 indx = bfd_get_symcount (output_bfd);
10249 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10256 if (bed->s->arch_size == 32)
10258 r_type_mask = 0xff;
10264 r_type_mask = 0xffffffff;
10269 /* Relocate the contents of each section. */
10270 sym_hashes = elf_sym_hashes (input_bfd);
10271 for (o = input_bfd->sections; o != NULL; o = o->next)
10273 bfd_byte *contents;
10275 if (! o->linker_mark)
10277 /* This section was omitted from the link. */
10281 if (bfd_link_relocatable (flinfo->info)
10282 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10284 /* Deal with the group signature symbol. */
10285 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10286 unsigned long symndx = sec_data->this_hdr.sh_info;
10287 asection *osec = o->output_section;
10289 if (symndx >= locsymcount
10290 || (elf_bad_symtab (input_bfd)
10291 && flinfo->sections[symndx] == NULL))
10293 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10294 while (h->root.type == bfd_link_hash_indirect
10295 || h->root.type == bfd_link_hash_warning)
10296 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10297 /* Arrange for symbol to be output. */
10299 elf_section_data (osec)->this_hdr.sh_info = -2;
10301 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10303 /* We'll use the output section target_index. */
10304 asection *sec = flinfo->sections[symndx]->output_section;
10305 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10309 if (flinfo->indices[symndx] == -1)
10311 /* Otherwise output the local symbol now. */
10312 Elf_Internal_Sym sym = isymbuf[symndx];
10313 asection *sec = flinfo->sections[symndx]->output_section;
10318 name = bfd_elf_string_from_elf_section (input_bfd,
10319 symtab_hdr->sh_link,
10324 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10326 if (sym.st_shndx == SHN_BAD)
10329 sym.st_value += o->output_offset;
10331 indx = bfd_get_symcount (output_bfd);
10332 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10337 flinfo->indices[symndx] = indx;
10341 elf_section_data (osec)->this_hdr.sh_info
10342 = flinfo->indices[symndx];
10346 if ((o->flags & SEC_HAS_CONTENTS) == 0
10347 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10350 if ((o->flags & SEC_LINKER_CREATED) != 0)
10352 /* Section was created by _bfd_elf_link_create_dynamic_sections
10357 /* Get the contents of the section. They have been cached by a
10358 relaxation routine. Note that o is a section in an input
10359 file, so the contents field will not have been set by any of
10360 the routines which work on output files. */
10361 if (elf_section_data (o)->this_hdr.contents != NULL)
10363 contents = elf_section_data (o)->this_hdr.contents;
10364 if (bed->caches_rawsize
10366 && o->rawsize < o->size)
10368 memcpy (flinfo->contents, contents, o->rawsize);
10369 contents = flinfo->contents;
10374 contents = flinfo->contents;
10375 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10379 if ((o->flags & SEC_RELOC) != 0)
10381 Elf_Internal_Rela *internal_relocs;
10382 Elf_Internal_Rela *rel, *relend;
10383 int action_discarded;
10386 /* Get the swapped relocs. */
10388 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10389 flinfo->internal_relocs, FALSE);
10390 if (internal_relocs == NULL
10391 && o->reloc_count > 0)
10394 /* We need to reverse-copy input .ctors/.dtors sections if
10395 they are placed in .init_array/.finit_array for output. */
10396 if (o->size > address_size
10397 && ((strncmp (o->name, ".ctors", 6) == 0
10398 && strcmp (o->output_section->name,
10399 ".init_array") == 0)
10400 || (strncmp (o->name, ".dtors", 6) == 0
10401 && strcmp (o->output_section->name,
10402 ".fini_array") == 0))
10403 && (o->name[6] == 0 || o->name[6] == '.'))
10405 if (o->size != o->reloc_count * address_size)
10408 /* xgettext:c-format */
10409 (_("error: %B: size of section %A is not "
10410 "multiple of address size"),
10412 bfd_set_error (bfd_error_on_input);
10415 o->flags |= SEC_ELF_REVERSE_COPY;
10418 action_discarded = -1;
10419 if (!elf_section_ignore_discarded_relocs (o))
10420 action_discarded = (*bed->action_discarded) (o);
10422 /* Run through the relocs evaluating complex reloc symbols and
10423 looking for relocs against symbols from discarded sections
10424 or section symbols from removed link-once sections.
10425 Complain about relocs against discarded sections. Zero
10426 relocs against removed link-once sections. */
10428 rel = internal_relocs;
10429 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10430 for ( ; rel < relend; rel++)
10432 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10433 unsigned int s_type;
10434 asection **ps, *sec;
10435 struct elf_link_hash_entry *h = NULL;
10436 const char *sym_name;
10438 if (r_symndx == STN_UNDEF)
10441 if (r_symndx >= locsymcount
10442 || (elf_bad_symtab (input_bfd)
10443 && flinfo->sections[r_symndx] == NULL))
10445 h = sym_hashes[r_symndx - extsymoff];
10447 /* Badly formatted input files can contain relocs that
10448 reference non-existant symbols. Check here so that
10449 we do not seg fault. */
10454 sprintf_vma (buffer, rel->r_info);
10456 /* xgettext:c-format */
10457 (_("error: %B contains a reloc (0x%s) for section %A "
10458 "that references a non-existent global symbol"),
10459 input_bfd, buffer, o);
10460 bfd_set_error (bfd_error_bad_value);
10464 while (h->root.type == bfd_link_hash_indirect
10465 || h->root.type == bfd_link_hash_warning)
10466 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10470 /* If a plugin symbol is referenced from a non-IR file,
10471 mark the symbol as undefined. Note that the
10472 linker may attach linker created dynamic sections
10473 to the plugin bfd. Symbols defined in linker
10474 created sections are not plugin symbols. */
10475 if (h->root.non_ir_ref
10476 && (h->root.type == bfd_link_hash_defined
10477 || h->root.type == bfd_link_hash_defweak)
10478 && (h->root.u.def.section->flags
10479 & SEC_LINKER_CREATED) == 0
10480 && h->root.u.def.section->owner != NULL
10481 && (h->root.u.def.section->owner->flags
10482 & BFD_PLUGIN) != 0)
10484 h->root.type = bfd_link_hash_undefined;
10485 h->root.u.undef.abfd = h->root.u.def.section->owner;
10489 if (h->root.type == bfd_link_hash_defined
10490 || h->root.type == bfd_link_hash_defweak)
10491 ps = &h->root.u.def.section;
10493 sym_name = h->root.root.string;
10497 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10499 s_type = ELF_ST_TYPE (sym->st_info);
10500 ps = &flinfo->sections[r_symndx];
10501 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10505 if ((s_type == STT_RELC || s_type == STT_SRELC)
10506 && !bfd_link_relocatable (flinfo->info))
10509 bfd_vma dot = (rel->r_offset
10510 + o->output_offset + o->output_section->vma);
10512 printf ("Encountered a complex symbol!");
10513 printf (" (input_bfd %s, section %s, reloc %ld\n",
10514 input_bfd->filename, o->name,
10515 (long) (rel - internal_relocs));
10516 printf (" symbol: idx %8.8lx, name %s\n",
10517 r_symndx, sym_name);
10518 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10519 (unsigned long) rel->r_info,
10520 (unsigned long) rel->r_offset);
10522 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10523 isymbuf, locsymcount, s_type == STT_SRELC))
10526 /* Symbol evaluated OK. Update to absolute value. */
10527 set_symbol_value (input_bfd, isymbuf, locsymcount,
10532 if (action_discarded != -1 && ps != NULL)
10534 /* Complain if the definition comes from a
10535 discarded section. */
10536 if ((sec = *ps) != NULL && discarded_section (sec))
10538 BFD_ASSERT (r_symndx != STN_UNDEF);
10539 if (action_discarded & COMPLAIN)
10540 (*flinfo->info->callbacks->einfo)
10541 /* xgettext:c-format */
10542 (_("%X`%s' referenced in section `%A' of %B: "
10543 "defined in discarded section `%A' of %B\n"),
10544 sym_name, o, input_bfd, sec, sec->owner);
10546 /* Try to do the best we can to support buggy old
10547 versions of gcc. Pretend that the symbol is
10548 really defined in the kept linkonce section.
10549 FIXME: This is quite broken. Modifying the
10550 symbol here means we will be changing all later
10551 uses of the symbol, not just in this section. */
10552 if (action_discarded & PRETEND)
10556 kept = _bfd_elf_check_kept_section (sec,
10568 /* Relocate the section by invoking a back end routine.
10570 The back end routine is responsible for adjusting the
10571 section contents as necessary, and (if using Rela relocs
10572 and generating a relocatable output file) adjusting the
10573 reloc addend as necessary.
10575 The back end routine does not have to worry about setting
10576 the reloc address or the reloc symbol index.
10578 The back end routine is given a pointer to the swapped in
10579 internal symbols, and can access the hash table entries
10580 for the external symbols via elf_sym_hashes (input_bfd).
10582 When generating relocatable output, the back end routine
10583 must handle STB_LOCAL/STT_SECTION symbols specially. The
10584 output symbol is going to be a section symbol
10585 corresponding to the output section, which will require
10586 the addend to be adjusted. */
10588 ret = (*relocate_section) (output_bfd, flinfo->info,
10589 input_bfd, o, contents,
10597 || bfd_link_relocatable (flinfo->info)
10598 || flinfo->info->emitrelocations)
10600 Elf_Internal_Rela *irela;
10601 Elf_Internal_Rela *irelaend, *irelamid;
10602 bfd_vma last_offset;
10603 struct elf_link_hash_entry **rel_hash;
10604 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10605 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10606 unsigned int next_erel;
10607 bfd_boolean rela_normal;
10608 struct bfd_elf_section_data *esdi, *esdo;
10610 esdi = elf_section_data (o);
10611 esdo = elf_section_data (o->output_section);
10612 rela_normal = FALSE;
10614 /* Adjust the reloc addresses and symbol indices. */
10616 irela = internal_relocs;
10617 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10618 rel_hash = esdo->rel.hashes + esdo->rel.count;
10619 /* We start processing the REL relocs, if any. When we reach
10620 IRELAMID in the loop, we switch to the RELA relocs. */
10622 if (esdi->rel.hdr != NULL)
10623 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10624 * bed->s->int_rels_per_ext_rel);
10625 rel_hash_list = rel_hash;
10626 rela_hash_list = NULL;
10627 last_offset = o->output_offset;
10628 if (!bfd_link_relocatable (flinfo->info))
10629 last_offset += o->output_section->vma;
10630 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10632 unsigned long r_symndx;
10634 Elf_Internal_Sym sym;
10636 if (next_erel == bed->s->int_rels_per_ext_rel)
10642 if (irela == irelamid)
10644 rel_hash = esdo->rela.hashes + esdo->rela.count;
10645 rela_hash_list = rel_hash;
10646 rela_normal = bed->rela_normal;
10649 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10652 if (irela->r_offset >= (bfd_vma) -2)
10654 /* This is a reloc for a deleted entry or somesuch.
10655 Turn it into an R_*_NONE reloc, at the same
10656 offset as the last reloc. elf_eh_frame.c and
10657 bfd_elf_discard_info rely on reloc offsets
10659 irela->r_offset = last_offset;
10661 irela->r_addend = 0;
10665 irela->r_offset += o->output_offset;
10667 /* Relocs in an executable have to be virtual addresses. */
10668 if (!bfd_link_relocatable (flinfo->info))
10669 irela->r_offset += o->output_section->vma;
10671 last_offset = irela->r_offset;
10673 r_symndx = irela->r_info >> r_sym_shift;
10674 if (r_symndx == STN_UNDEF)
10677 if (r_symndx >= locsymcount
10678 || (elf_bad_symtab (input_bfd)
10679 && flinfo->sections[r_symndx] == NULL))
10681 struct elf_link_hash_entry *rh;
10682 unsigned long indx;
10684 /* This is a reloc against a global symbol. We
10685 have not yet output all the local symbols, so
10686 we do not know the symbol index of any global
10687 symbol. We set the rel_hash entry for this
10688 reloc to point to the global hash table entry
10689 for this symbol. The symbol index is then
10690 set at the end of bfd_elf_final_link. */
10691 indx = r_symndx - extsymoff;
10692 rh = elf_sym_hashes (input_bfd)[indx];
10693 while (rh->root.type == bfd_link_hash_indirect
10694 || rh->root.type == bfd_link_hash_warning)
10695 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10697 /* Setting the index to -2 tells
10698 elf_link_output_extsym that this symbol is
10699 used by a reloc. */
10700 BFD_ASSERT (rh->indx < 0);
10708 /* This is a reloc against a local symbol. */
10711 sym = isymbuf[r_symndx];
10712 sec = flinfo->sections[r_symndx];
10713 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10715 /* I suppose the backend ought to fill in the
10716 section of any STT_SECTION symbol against a
10717 processor specific section. */
10718 r_symndx = STN_UNDEF;
10719 if (bfd_is_abs_section (sec))
10721 else if (sec == NULL || sec->owner == NULL)
10723 bfd_set_error (bfd_error_bad_value);
10728 asection *osec = sec->output_section;
10730 /* If we have discarded a section, the output
10731 section will be the absolute section. In
10732 case of discarded SEC_MERGE sections, use
10733 the kept section. relocate_section should
10734 have already handled discarded linkonce
10736 if (bfd_is_abs_section (osec)
10737 && sec->kept_section != NULL
10738 && sec->kept_section->output_section != NULL)
10740 osec = sec->kept_section->output_section;
10741 irela->r_addend -= osec->vma;
10744 if (!bfd_is_abs_section (osec))
10746 r_symndx = osec->target_index;
10747 if (r_symndx == STN_UNDEF)
10749 irela->r_addend += osec->vma;
10750 osec = _bfd_nearby_section (output_bfd, osec,
10752 irela->r_addend -= osec->vma;
10753 r_symndx = osec->target_index;
10758 /* Adjust the addend according to where the
10759 section winds up in the output section. */
10761 irela->r_addend += sec->output_offset;
10765 if (flinfo->indices[r_symndx] == -1)
10767 unsigned long shlink;
10772 if (flinfo->info->strip == strip_all)
10774 /* You can't do ld -r -s. */
10775 bfd_set_error (bfd_error_invalid_operation);
10779 /* This symbol was skipped earlier, but
10780 since it is needed by a reloc, we
10781 must output it now. */
10782 shlink = symtab_hdr->sh_link;
10783 name = (bfd_elf_string_from_elf_section
10784 (input_bfd, shlink, sym.st_name));
10788 osec = sec->output_section;
10790 _bfd_elf_section_from_bfd_section (output_bfd,
10792 if (sym.st_shndx == SHN_BAD)
10795 sym.st_value += sec->output_offset;
10796 if (!bfd_link_relocatable (flinfo->info))
10798 sym.st_value += osec->vma;
10799 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10801 /* STT_TLS symbols are relative to PT_TLS
10803 BFD_ASSERT (elf_hash_table (flinfo->info)
10804 ->tls_sec != NULL);
10805 sym.st_value -= (elf_hash_table (flinfo->info)
10810 indx = bfd_get_symcount (output_bfd);
10811 ret = elf_link_output_symstrtab (flinfo, name,
10817 flinfo->indices[r_symndx] = indx;
10822 r_symndx = flinfo->indices[r_symndx];
10825 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10826 | (irela->r_info & r_type_mask));
10829 /* Swap out the relocs. */
10830 input_rel_hdr = esdi->rel.hdr;
10831 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10833 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10838 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10839 * bed->s->int_rels_per_ext_rel);
10840 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10843 input_rela_hdr = esdi->rela.hdr;
10844 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10846 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10855 /* Write out the modified section contents. */
10856 if (bed->elf_backend_write_section
10857 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10860 /* Section written out. */
10862 else switch (o->sec_info_type)
10864 case SEC_INFO_TYPE_STABS:
10865 if (! (_bfd_write_section_stabs
10867 &elf_hash_table (flinfo->info)->stab_info,
10868 o, &elf_section_data (o)->sec_info, contents)))
10871 case SEC_INFO_TYPE_MERGE:
10872 if (! _bfd_write_merged_section (output_bfd, o,
10873 elf_section_data (o)->sec_info))
10876 case SEC_INFO_TYPE_EH_FRAME:
10878 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10883 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10885 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10893 if (! (o->flags & SEC_EXCLUDE))
10895 file_ptr offset = (file_ptr) o->output_offset;
10896 bfd_size_type todo = o->size;
10898 offset *= bfd_octets_per_byte (output_bfd);
10900 if ((o->flags & SEC_ELF_REVERSE_COPY))
10902 /* Reverse-copy input section to output. */
10905 todo -= address_size;
10906 if (! bfd_set_section_contents (output_bfd,
10914 offset += address_size;
10918 else if (! bfd_set_section_contents (output_bfd,
10932 /* Generate a reloc when linking an ELF file. This is a reloc
10933 requested by the linker, and does not come from any input file. This
10934 is used to build constructor and destructor tables when linking
10938 elf_reloc_link_order (bfd *output_bfd,
10939 struct bfd_link_info *info,
10940 asection *output_section,
10941 struct bfd_link_order *link_order)
10943 reloc_howto_type *howto;
10947 struct bfd_elf_section_reloc_data *reldata;
10948 struct elf_link_hash_entry **rel_hash_ptr;
10949 Elf_Internal_Shdr *rel_hdr;
10950 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10951 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10954 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10956 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10959 bfd_set_error (bfd_error_bad_value);
10963 addend = link_order->u.reloc.p->addend;
10966 reldata = &esdo->rel;
10967 else if (esdo->rela.hdr)
10968 reldata = &esdo->rela;
10975 /* Figure out the symbol index. */
10976 rel_hash_ptr = reldata->hashes + reldata->count;
10977 if (link_order->type == bfd_section_reloc_link_order)
10979 indx = link_order->u.reloc.p->u.section->target_index;
10980 BFD_ASSERT (indx != 0);
10981 *rel_hash_ptr = NULL;
10985 struct elf_link_hash_entry *h;
10987 /* Treat a reloc against a defined symbol as though it were
10988 actually against the section. */
10989 h = ((struct elf_link_hash_entry *)
10990 bfd_wrapped_link_hash_lookup (output_bfd, info,
10991 link_order->u.reloc.p->u.name,
10992 FALSE, FALSE, TRUE));
10994 && (h->root.type == bfd_link_hash_defined
10995 || h->root.type == bfd_link_hash_defweak))
10999 section = h->root.u.def.section;
11000 indx = section->output_section->target_index;
11001 *rel_hash_ptr = NULL;
11002 /* It seems that we ought to add the symbol value to the
11003 addend here, but in practice it has already been added
11004 because it was passed to constructor_callback. */
11005 addend += section->output_section->vma + section->output_offset;
11007 else if (h != NULL)
11009 /* Setting the index to -2 tells elf_link_output_extsym that
11010 this symbol is used by a reloc. */
11017 (*info->callbacks->unattached_reloc)
11018 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11023 /* If this is an inplace reloc, we must write the addend into the
11025 if (howto->partial_inplace && addend != 0)
11027 bfd_size_type size;
11028 bfd_reloc_status_type rstat;
11031 const char *sym_name;
11033 size = (bfd_size_type) bfd_get_reloc_size (howto);
11034 buf = (bfd_byte *) bfd_zmalloc (size);
11035 if (buf == NULL && size != 0)
11037 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11044 case bfd_reloc_outofrange:
11047 case bfd_reloc_overflow:
11048 if (link_order->type == bfd_section_reloc_link_order)
11049 sym_name = bfd_section_name (output_bfd,
11050 link_order->u.reloc.p->u.section);
11052 sym_name = link_order->u.reloc.p->u.name;
11053 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11054 howto->name, addend, NULL, NULL,
11059 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11061 * bfd_octets_per_byte (output_bfd),
11068 /* The address of a reloc is relative to the section in a
11069 relocatable file, and is a virtual address in an executable
11071 offset = link_order->offset;
11072 if (! bfd_link_relocatable (info))
11073 offset += output_section->vma;
11075 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11077 irel[i].r_offset = offset;
11078 irel[i].r_info = 0;
11079 irel[i].r_addend = 0;
11081 if (bed->s->arch_size == 32)
11082 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11084 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11086 rel_hdr = reldata->hdr;
11087 erel = rel_hdr->contents;
11088 if (rel_hdr->sh_type == SHT_REL)
11090 erel += reldata->count * bed->s->sizeof_rel;
11091 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11095 irel[0].r_addend = addend;
11096 erel += reldata->count * bed->s->sizeof_rela;
11097 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11106 /* Get the output vma of the section pointed to by the sh_link field. */
11109 elf_get_linked_section_vma (struct bfd_link_order *p)
11111 Elf_Internal_Shdr **elf_shdrp;
11115 s = p->u.indirect.section;
11116 elf_shdrp = elf_elfsections (s->owner);
11117 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11118 elfsec = elf_shdrp[elfsec]->sh_link;
11120 The Intel C compiler generates SHT_IA_64_UNWIND with
11121 SHF_LINK_ORDER. But it doesn't set the sh_link or
11122 sh_info fields. Hence we could get the situation
11123 where elfsec is 0. */
11126 const struct elf_backend_data *bed
11127 = get_elf_backend_data (s->owner);
11128 if (bed->link_order_error_handler)
11129 bed->link_order_error_handler
11130 /* xgettext:c-format */
11131 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
11136 s = elf_shdrp[elfsec]->bfd_section;
11137 return s->output_section->vma + s->output_offset;
11142 /* Compare two sections based on the locations of the sections they are
11143 linked to. Used by elf_fixup_link_order. */
11146 compare_link_order (const void * a, const void * b)
11151 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11152 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11155 return apos > bpos;
11159 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11160 order as their linked sections. Returns false if this could not be done
11161 because an output section includes both ordered and unordered
11162 sections. Ideally we'd do this in the linker proper. */
11165 elf_fixup_link_order (bfd *abfd, asection *o)
11167 int seen_linkorder;
11170 struct bfd_link_order *p;
11172 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11174 struct bfd_link_order **sections;
11175 asection *s, *other_sec, *linkorder_sec;
11179 linkorder_sec = NULL;
11181 seen_linkorder = 0;
11182 for (p = o->map_head.link_order; p != NULL; p = p->next)
11184 if (p->type == bfd_indirect_link_order)
11186 s = p->u.indirect.section;
11188 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11189 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11190 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11191 && elfsec < elf_numsections (sub)
11192 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11193 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11207 if (seen_other && seen_linkorder)
11209 if (other_sec && linkorder_sec)
11211 /* xgettext:c-format */
11212 (_("%A has both ordered [`%A' in %B] "
11213 "and unordered [`%A' in %B] sections"),
11214 o, linkorder_sec, linkorder_sec->owner,
11215 other_sec, other_sec->owner);
11218 (_("%A has both ordered and unordered sections"), o);
11219 bfd_set_error (bfd_error_bad_value);
11224 if (!seen_linkorder)
11227 sections = (struct bfd_link_order **)
11228 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11229 if (sections == NULL)
11231 seen_linkorder = 0;
11233 for (p = o->map_head.link_order; p != NULL; p = p->next)
11235 sections[seen_linkorder++] = p;
11237 /* Sort the input sections in the order of their linked section. */
11238 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11239 compare_link_order);
11241 /* Change the offsets of the sections. */
11243 for (n = 0; n < seen_linkorder; n++)
11245 s = sections[n]->u.indirect.section;
11246 offset &= ~(bfd_vma) 0 << s->alignment_power;
11247 s->output_offset = offset / bfd_octets_per_byte (abfd);
11248 sections[n]->offset = offset;
11249 offset += sections[n]->size;
11256 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11257 Returns TRUE upon success, FALSE otherwise. */
11260 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11262 bfd_boolean ret = FALSE;
11264 const struct elf_backend_data *bed;
11266 enum bfd_architecture arch;
11268 asymbol **sympp = NULL;
11272 elf_symbol_type *osymbuf;
11274 implib_bfd = info->out_implib_bfd;
11275 bed = get_elf_backend_data (abfd);
11277 if (!bfd_set_format (implib_bfd, bfd_object))
11280 /* Use flag from executable but make it a relocatable object. */
11281 flags = bfd_get_file_flags (abfd);
11282 flags &= ~HAS_RELOC;
11283 if (!bfd_set_start_address (implib_bfd, 0)
11284 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11287 /* Copy architecture of output file to import library file. */
11288 arch = bfd_get_arch (abfd);
11289 mach = bfd_get_mach (abfd);
11290 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11291 && (abfd->target_defaulted
11292 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11295 /* Get symbol table size. */
11296 symsize = bfd_get_symtab_upper_bound (abfd);
11300 /* Read in the symbol table. */
11301 sympp = (asymbol **) xmalloc (symsize);
11302 symcount = bfd_canonicalize_symtab (abfd, sympp);
11306 /* Allow the BFD backend to copy any private header data it
11307 understands from the output BFD to the import library BFD. */
11308 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11311 /* Filter symbols to appear in the import library. */
11312 if (bed->elf_backend_filter_implib_symbols)
11313 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11316 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11319 bfd_set_error (bfd_error_no_symbols);
11320 _bfd_error_handler (_("%B: no symbol found for import library"),
11326 /* Make symbols absolute. */
11327 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11328 sizeof (*osymbuf));
11329 for (src_count = 0; src_count < symcount; src_count++)
11331 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11332 sizeof (*osymbuf));
11333 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11334 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11335 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11336 osymbuf[src_count].internal_elf_sym.st_value =
11337 osymbuf[src_count].symbol.value;
11338 sympp[src_count] = &osymbuf[src_count].symbol;
11341 bfd_set_symtab (implib_bfd, sympp, symcount);
11343 /* Allow the BFD backend to copy any private data it understands
11344 from the output BFD to the import library BFD. This is done last
11345 to permit the routine to look at the filtered symbol table. */
11346 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11349 if (!bfd_close (implib_bfd))
11360 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11364 if (flinfo->symstrtab != NULL)
11365 _bfd_elf_strtab_free (flinfo->symstrtab);
11366 if (flinfo->contents != NULL)
11367 free (flinfo->contents);
11368 if (flinfo->external_relocs != NULL)
11369 free (flinfo->external_relocs);
11370 if (flinfo->internal_relocs != NULL)
11371 free (flinfo->internal_relocs);
11372 if (flinfo->external_syms != NULL)
11373 free (flinfo->external_syms);
11374 if (flinfo->locsym_shndx != NULL)
11375 free (flinfo->locsym_shndx);
11376 if (flinfo->internal_syms != NULL)
11377 free (flinfo->internal_syms);
11378 if (flinfo->indices != NULL)
11379 free (flinfo->indices);
11380 if (flinfo->sections != NULL)
11381 free (flinfo->sections);
11382 if (flinfo->symshndxbuf != NULL)
11383 free (flinfo->symshndxbuf);
11384 for (o = obfd->sections; o != NULL; o = o->next)
11386 struct bfd_elf_section_data *esdo = elf_section_data (o);
11387 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11388 free (esdo->rel.hashes);
11389 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11390 free (esdo->rela.hashes);
11394 /* Do the final step of an ELF link. */
11397 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11399 bfd_boolean dynamic;
11400 bfd_boolean emit_relocs;
11402 struct elf_final_link_info flinfo;
11404 struct bfd_link_order *p;
11406 bfd_size_type max_contents_size;
11407 bfd_size_type max_external_reloc_size;
11408 bfd_size_type max_internal_reloc_count;
11409 bfd_size_type max_sym_count;
11410 bfd_size_type max_sym_shndx_count;
11411 Elf_Internal_Sym elfsym;
11413 Elf_Internal_Shdr *symtab_hdr;
11414 Elf_Internal_Shdr *symtab_shndx_hdr;
11415 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11416 struct elf_outext_info eoinfo;
11417 bfd_boolean merged;
11418 size_t relativecount = 0;
11419 asection *reldyn = 0;
11421 asection *attr_section = NULL;
11422 bfd_vma attr_size = 0;
11423 const char *std_attrs_section;
11424 struct elf_link_hash_table *htab = elf_hash_table (info);
11426 if (!is_elf_hash_table (htab))
11429 if (bfd_link_pic (info))
11430 abfd->flags |= DYNAMIC;
11432 dynamic = htab->dynamic_sections_created;
11433 dynobj = htab->dynobj;
11435 emit_relocs = (bfd_link_relocatable (info)
11436 || info->emitrelocations);
11438 flinfo.info = info;
11439 flinfo.output_bfd = abfd;
11440 flinfo.symstrtab = _bfd_elf_strtab_init ();
11441 if (flinfo.symstrtab == NULL)
11446 flinfo.hash_sec = NULL;
11447 flinfo.symver_sec = NULL;
11451 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11452 /* Note that dynsym_sec can be NULL (on VMS). */
11453 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11454 /* Note that it is OK if symver_sec is NULL. */
11457 flinfo.contents = NULL;
11458 flinfo.external_relocs = NULL;
11459 flinfo.internal_relocs = NULL;
11460 flinfo.external_syms = NULL;
11461 flinfo.locsym_shndx = NULL;
11462 flinfo.internal_syms = NULL;
11463 flinfo.indices = NULL;
11464 flinfo.sections = NULL;
11465 flinfo.symshndxbuf = NULL;
11466 flinfo.filesym_count = 0;
11468 /* The object attributes have been merged. Remove the input
11469 sections from the link, and set the contents of the output
11471 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11472 for (o = abfd->sections; o != NULL; o = o->next)
11474 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11475 || strcmp (o->name, ".gnu.attributes") == 0)
11477 for (p = o->map_head.link_order; p != NULL; p = p->next)
11479 asection *input_section;
11481 if (p->type != bfd_indirect_link_order)
11483 input_section = p->u.indirect.section;
11484 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11485 elf_link_input_bfd ignores this section. */
11486 input_section->flags &= ~SEC_HAS_CONTENTS;
11489 attr_size = bfd_elf_obj_attr_size (abfd);
11492 bfd_set_section_size (abfd, o, attr_size);
11494 /* Skip this section later on. */
11495 o->map_head.link_order = NULL;
11498 o->flags |= SEC_EXCLUDE;
11502 /* Count up the number of relocations we will output for each output
11503 section, so that we know the sizes of the reloc sections. We
11504 also figure out some maximum sizes. */
11505 max_contents_size = 0;
11506 max_external_reloc_size = 0;
11507 max_internal_reloc_count = 0;
11509 max_sym_shndx_count = 0;
11511 for (o = abfd->sections; o != NULL; o = o->next)
11513 struct bfd_elf_section_data *esdo = elf_section_data (o);
11514 o->reloc_count = 0;
11516 for (p = o->map_head.link_order; p != NULL; p = p->next)
11518 unsigned int reloc_count = 0;
11519 unsigned int additional_reloc_count = 0;
11520 struct bfd_elf_section_data *esdi = NULL;
11522 if (p->type == bfd_section_reloc_link_order
11523 || p->type == bfd_symbol_reloc_link_order)
11525 else if (p->type == bfd_indirect_link_order)
11529 sec = p->u.indirect.section;
11531 /* Mark all sections which are to be included in the
11532 link. This will normally be every section. We need
11533 to do this so that we can identify any sections which
11534 the linker has decided to not include. */
11535 sec->linker_mark = TRUE;
11537 if (sec->flags & SEC_MERGE)
11540 if (sec->rawsize > max_contents_size)
11541 max_contents_size = sec->rawsize;
11542 if (sec->size > max_contents_size)
11543 max_contents_size = sec->size;
11545 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11546 && (sec->owner->flags & DYNAMIC) == 0)
11550 /* We are interested in just local symbols, not all
11552 if (elf_bad_symtab (sec->owner))
11553 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11554 / bed->s->sizeof_sym);
11556 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11558 if (sym_count > max_sym_count)
11559 max_sym_count = sym_count;
11561 if (sym_count > max_sym_shndx_count
11562 && elf_symtab_shndx_list (sec->owner) != NULL)
11563 max_sym_shndx_count = sym_count;
11565 if (esdo->this_hdr.sh_type == SHT_REL
11566 || esdo->this_hdr.sh_type == SHT_RELA)
11567 /* Some backends use reloc_count in relocation sections
11568 to count particular types of relocs. Of course,
11569 reloc sections themselves can't have relocations. */
11571 else if (emit_relocs)
11573 reloc_count = sec->reloc_count;
11574 if (bed->elf_backend_count_additional_relocs)
11577 c = (*bed->elf_backend_count_additional_relocs) (sec);
11578 additional_reloc_count += c;
11581 else if (bed->elf_backend_count_relocs)
11582 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11584 esdi = elf_section_data (sec);
11586 if ((sec->flags & SEC_RELOC) != 0)
11588 size_t ext_size = 0;
11590 if (esdi->rel.hdr != NULL)
11591 ext_size = esdi->rel.hdr->sh_size;
11592 if (esdi->rela.hdr != NULL)
11593 ext_size += esdi->rela.hdr->sh_size;
11595 if (ext_size > max_external_reloc_size)
11596 max_external_reloc_size = ext_size;
11597 if (sec->reloc_count > max_internal_reloc_count)
11598 max_internal_reloc_count = sec->reloc_count;
11603 if (reloc_count == 0)
11606 reloc_count += additional_reloc_count;
11607 o->reloc_count += reloc_count;
11609 if (p->type == bfd_indirect_link_order && emit_relocs)
11613 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11614 esdo->rel.count += additional_reloc_count;
11616 if (esdi->rela.hdr)
11618 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11619 esdo->rela.count += additional_reloc_count;
11625 esdo->rela.count += reloc_count;
11627 esdo->rel.count += reloc_count;
11631 if (o->reloc_count > 0)
11632 o->flags |= SEC_RELOC;
11635 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11636 set it (this is probably a bug) and if it is set
11637 assign_section_numbers will create a reloc section. */
11638 o->flags &=~ SEC_RELOC;
11641 /* If the SEC_ALLOC flag is not set, force the section VMA to
11642 zero. This is done in elf_fake_sections as well, but forcing
11643 the VMA to 0 here will ensure that relocs against these
11644 sections are handled correctly. */
11645 if ((o->flags & SEC_ALLOC) == 0
11646 && ! o->user_set_vma)
11650 if (! bfd_link_relocatable (info) && merged)
11651 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11653 /* Figure out the file positions for everything but the symbol table
11654 and the relocs. We set symcount to force assign_section_numbers
11655 to create a symbol table. */
11656 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11657 BFD_ASSERT (! abfd->output_has_begun);
11658 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11661 /* Set sizes, and assign file positions for reloc sections. */
11662 for (o = abfd->sections; o != NULL; o = o->next)
11664 struct bfd_elf_section_data *esdo = elf_section_data (o);
11665 if ((o->flags & SEC_RELOC) != 0)
11668 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11672 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11676 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11677 to count upwards while actually outputting the relocations. */
11678 esdo->rel.count = 0;
11679 esdo->rela.count = 0;
11681 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11683 /* Cache the section contents so that they can be compressed
11684 later. Use bfd_malloc since it will be freed by
11685 bfd_compress_section_contents. */
11686 unsigned char *contents = esdo->this_hdr.contents;
11687 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11690 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11691 if (contents == NULL)
11693 esdo->this_hdr.contents = contents;
11697 /* We have now assigned file positions for all the sections except
11698 .symtab, .strtab, and non-loaded reloc sections. We start the
11699 .symtab section at the current file position, and write directly
11700 to it. We build the .strtab section in memory. */
11701 bfd_get_symcount (abfd) = 0;
11702 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11703 /* sh_name is set in prep_headers. */
11704 symtab_hdr->sh_type = SHT_SYMTAB;
11705 /* sh_flags, sh_addr and sh_size all start off zero. */
11706 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11707 /* sh_link is set in assign_section_numbers. */
11708 /* sh_info is set below. */
11709 /* sh_offset is set just below. */
11710 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11712 if (max_sym_count < 20)
11713 max_sym_count = 20;
11714 htab->strtabsize = max_sym_count;
11715 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11716 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11717 if (htab->strtab == NULL)
11719 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11721 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11722 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11724 if (info->strip != strip_all || emit_relocs)
11726 file_ptr off = elf_next_file_pos (abfd);
11728 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11730 /* Note that at this point elf_next_file_pos (abfd) is
11731 incorrect. We do not yet know the size of the .symtab section.
11732 We correct next_file_pos below, after we do know the size. */
11734 /* Start writing out the symbol table. The first symbol is always a
11736 elfsym.st_value = 0;
11737 elfsym.st_size = 0;
11738 elfsym.st_info = 0;
11739 elfsym.st_other = 0;
11740 elfsym.st_shndx = SHN_UNDEF;
11741 elfsym.st_target_internal = 0;
11742 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11743 bfd_und_section_ptr, NULL) != 1)
11746 /* Output a symbol for each section. We output these even if we are
11747 discarding local symbols, since they are used for relocs. These
11748 symbols have no names. We store the index of each one in the
11749 index field of the section, so that we can find it again when
11750 outputting relocs. */
11752 elfsym.st_size = 0;
11753 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11754 elfsym.st_other = 0;
11755 elfsym.st_value = 0;
11756 elfsym.st_target_internal = 0;
11757 for (i = 1; i < elf_numsections (abfd); i++)
11759 o = bfd_section_from_elf_index (abfd, i);
11762 o->target_index = bfd_get_symcount (abfd);
11763 elfsym.st_shndx = i;
11764 if (!bfd_link_relocatable (info))
11765 elfsym.st_value = o->vma;
11766 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11773 /* Allocate some memory to hold information read in from the input
11775 if (max_contents_size != 0)
11777 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11778 if (flinfo.contents == NULL)
11782 if (max_external_reloc_size != 0)
11784 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11785 if (flinfo.external_relocs == NULL)
11789 if (max_internal_reloc_count != 0)
11791 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11792 amt *= sizeof (Elf_Internal_Rela);
11793 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11794 if (flinfo.internal_relocs == NULL)
11798 if (max_sym_count != 0)
11800 amt = max_sym_count * bed->s->sizeof_sym;
11801 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11802 if (flinfo.external_syms == NULL)
11805 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11806 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11807 if (flinfo.internal_syms == NULL)
11810 amt = max_sym_count * sizeof (long);
11811 flinfo.indices = (long int *) bfd_malloc (amt);
11812 if (flinfo.indices == NULL)
11815 amt = max_sym_count * sizeof (asection *);
11816 flinfo.sections = (asection **) bfd_malloc (amt);
11817 if (flinfo.sections == NULL)
11821 if (max_sym_shndx_count != 0)
11823 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11824 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11825 if (flinfo.locsym_shndx == NULL)
11831 bfd_vma base, end = 0;
11834 for (sec = htab->tls_sec;
11835 sec && (sec->flags & SEC_THREAD_LOCAL);
11838 bfd_size_type size = sec->size;
11841 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11843 struct bfd_link_order *ord = sec->map_tail.link_order;
11846 size = ord->offset + ord->size;
11848 end = sec->vma + size;
11850 base = htab->tls_sec->vma;
11851 /* Only align end of TLS section if static TLS doesn't have special
11852 alignment requirements. */
11853 if (bed->static_tls_alignment == 1)
11854 end = align_power (end, htab->tls_sec->alignment_power);
11855 htab->tls_size = end - base;
11858 /* Reorder SHF_LINK_ORDER sections. */
11859 for (o = abfd->sections; o != NULL; o = o->next)
11861 if (!elf_fixup_link_order (abfd, o))
11865 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11868 /* Since ELF permits relocations to be against local symbols, we
11869 must have the local symbols available when we do the relocations.
11870 Since we would rather only read the local symbols once, and we
11871 would rather not keep them in memory, we handle all the
11872 relocations for a single input file at the same time.
11874 Unfortunately, there is no way to know the total number of local
11875 symbols until we have seen all of them, and the local symbol
11876 indices precede the global symbol indices. This means that when
11877 we are generating relocatable output, and we see a reloc against
11878 a global symbol, we can not know the symbol index until we have
11879 finished examining all the local symbols to see which ones we are
11880 going to output. To deal with this, we keep the relocations in
11881 memory, and don't output them until the end of the link. This is
11882 an unfortunate waste of memory, but I don't see a good way around
11883 it. Fortunately, it only happens when performing a relocatable
11884 link, which is not the common case. FIXME: If keep_memory is set
11885 we could write the relocs out and then read them again; I don't
11886 know how bad the memory loss will be. */
11888 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11889 sub->output_has_begun = FALSE;
11890 for (o = abfd->sections; o != NULL; o = o->next)
11892 for (p = o->map_head.link_order; p != NULL; p = p->next)
11894 if (p->type == bfd_indirect_link_order
11895 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11896 == bfd_target_elf_flavour)
11897 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11899 if (! sub->output_has_begun)
11901 if (! elf_link_input_bfd (&flinfo, sub))
11903 sub->output_has_begun = TRUE;
11906 else if (p->type == bfd_section_reloc_link_order
11907 || p->type == bfd_symbol_reloc_link_order)
11909 if (! elf_reloc_link_order (abfd, info, o, p))
11914 if (! _bfd_default_link_order (abfd, info, o, p))
11916 if (p->type == bfd_indirect_link_order
11917 && (bfd_get_flavour (sub)
11918 == bfd_target_elf_flavour)
11919 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11920 != bed->s->elfclass))
11922 const char *iclass, *oclass;
11924 switch (bed->s->elfclass)
11926 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11927 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11928 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11932 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11934 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11935 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11936 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11940 bfd_set_error (bfd_error_wrong_format);
11942 /* xgettext:c-format */
11943 (_("%B: file class %s incompatible with %s"),
11944 sub, iclass, oclass);
11953 /* Free symbol buffer if needed. */
11954 if (!info->reduce_memory_overheads)
11956 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11957 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11958 && elf_tdata (sub)->symbuf)
11960 free (elf_tdata (sub)->symbuf);
11961 elf_tdata (sub)->symbuf = NULL;
11965 /* Output any global symbols that got converted to local in a
11966 version script or due to symbol visibility. We do this in a
11967 separate step since ELF requires all local symbols to appear
11968 prior to any global symbols. FIXME: We should only do this if
11969 some global symbols were, in fact, converted to become local.
11970 FIXME: Will this work correctly with the Irix 5 linker? */
11971 eoinfo.failed = FALSE;
11972 eoinfo.flinfo = &flinfo;
11973 eoinfo.localsyms = TRUE;
11974 eoinfo.file_sym_done = FALSE;
11975 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11979 /* If backend needs to output some local symbols not present in the hash
11980 table, do it now. */
11981 if (bed->elf_backend_output_arch_local_syms
11982 && (info->strip != strip_all || emit_relocs))
11984 typedef int (*out_sym_func)
11985 (void *, const char *, Elf_Internal_Sym *, asection *,
11986 struct elf_link_hash_entry *);
11988 if (! ((*bed->elf_backend_output_arch_local_syms)
11989 (abfd, info, &flinfo,
11990 (out_sym_func) elf_link_output_symstrtab)))
11994 /* That wrote out all the local symbols. Finish up the symbol table
11995 with the global symbols. Even if we want to strip everything we
11996 can, we still need to deal with those global symbols that got
11997 converted to local in a version script. */
11999 /* The sh_info field records the index of the first non local symbol. */
12000 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12003 && htab->dynsym != NULL
12004 && htab->dynsym->output_section != bfd_abs_section_ptr)
12006 Elf_Internal_Sym sym;
12007 bfd_byte *dynsym = htab->dynsym->contents;
12009 o = htab->dynsym->output_section;
12010 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12012 /* Write out the section symbols for the output sections. */
12013 if (bfd_link_pic (info)
12014 || htab->is_relocatable_executable)
12020 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12022 sym.st_target_internal = 0;
12024 for (s = abfd->sections; s != NULL; s = s->next)
12030 dynindx = elf_section_data (s)->dynindx;
12033 indx = elf_section_data (s)->this_idx;
12034 BFD_ASSERT (indx > 0);
12035 sym.st_shndx = indx;
12036 if (! check_dynsym (abfd, &sym))
12038 sym.st_value = s->vma;
12039 dest = dynsym + dynindx * bed->s->sizeof_sym;
12040 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12044 /* Write out the local dynsyms. */
12045 if (htab->dynlocal)
12047 struct elf_link_local_dynamic_entry *e;
12048 for (e = htab->dynlocal; e ; e = e->next)
12053 /* Copy the internal symbol and turn off visibility.
12054 Note that we saved a word of storage and overwrote
12055 the original st_name with the dynstr_index. */
12057 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12059 s = bfd_section_from_elf_index (e->input_bfd,
12064 elf_section_data (s->output_section)->this_idx;
12065 if (! check_dynsym (abfd, &sym))
12067 sym.st_value = (s->output_section->vma
12069 + e->isym.st_value);
12072 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12073 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12078 /* We get the global symbols from the hash table. */
12079 eoinfo.failed = FALSE;
12080 eoinfo.localsyms = FALSE;
12081 eoinfo.flinfo = &flinfo;
12082 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12086 /* If backend needs to output some symbols not present in the hash
12087 table, do it now. */
12088 if (bed->elf_backend_output_arch_syms
12089 && (info->strip != strip_all || emit_relocs))
12091 typedef int (*out_sym_func)
12092 (void *, const char *, Elf_Internal_Sym *, asection *,
12093 struct elf_link_hash_entry *);
12095 if (! ((*bed->elf_backend_output_arch_syms)
12096 (abfd, info, &flinfo,
12097 (out_sym_func) elf_link_output_symstrtab)))
12101 /* Finalize the .strtab section. */
12102 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12104 /* Swap out the .strtab section. */
12105 if (!elf_link_swap_symbols_out (&flinfo))
12108 /* Now we know the size of the symtab section. */
12109 if (bfd_get_symcount (abfd) > 0)
12111 /* Finish up and write out the symbol string table (.strtab)
12113 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12114 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12116 if (elf_symtab_shndx_list (abfd))
12118 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12120 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12122 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12123 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12124 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12125 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12126 symtab_shndx_hdr->sh_size = amt;
12128 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12131 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12132 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12137 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12138 /* sh_name was set in prep_headers. */
12139 symstrtab_hdr->sh_type = SHT_STRTAB;
12140 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12141 symstrtab_hdr->sh_addr = 0;
12142 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12143 symstrtab_hdr->sh_entsize = 0;
12144 symstrtab_hdr->sh_link = 0;
12145 symstrtab_hdr->sh_info = 0;
12146 /* sh_offset is set just below. */
12147 symstrtab_hdr->sh_addralign = 1;
12149 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12151 elf_next_file_pos (abfd) = off;
12153 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12154 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12158 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12160 _bfd_error_handler (_("%B: failed to generate import library"),
12161 info->out_implib_bfd);
12165 /* Adjust the relocs to have the correct symbol indices. */
12166 for (o = abfd->sections; o != NULL; o = o->next)
12168 struct bfd_elf_section_data *esdo = elf_section_data (o);
12170 if ((o->flags & SEC_RELOC) == 0)
12173 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12174 if (esdo->rel.hdr != NULL
12175 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
12177 if (esdo->rela.hdr != NULL
12178 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
12181 /* Set the reloc_count field to 0 to prevent write_relocs from
12182 trying to swap the relocs out itself. */
12183 o->reloc_count = 0;
12186 if (dynamic && info->combreloc && dynobj != NULL)
12187 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12189 /* If we are linking against a dynamic object, or generating a
12190 shared library, finish up the dynamic linking information. */
12193 bfd_byte *dyncon, *dynconend;
12195 /* Fix up .dynamic entries. */
12196 o = bfd_get_linker_section (dynobj, ".dynamic");
12197 BFD_ASSERT (o != NULL);
12199 dyncon = o->contents;
12200 dynconend = o->contents + o->size;
12201 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12203 Elf_Internal_Dyn dyn;
12206 bfd_size_type sh_size;
12209 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12216 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12218 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12220 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12221 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12224 dyn.d_un.d_val = relativecount;
12231 name = info->init_function;
12234 name = info->fini_function;
12237 struct elf_link_hash_entry *h;
12239 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12241 && (h->root.type == bfd_link_hash_defined
12242 || h->root.type == bfd_link_hash_defweak))
12244 dyn.d_un.d_ptr = h->root.u.def.value;
12245 o = h->root.u.def.section;
12246 if (o->output_section != NULL)
12247 dyn.d_un.d_ptr += (o->output_section->vma
12248 + o->output_offset);
12251 /* The symbol is imported from another shared
12252 library and does not apply to this one. */
12253 dyn.d_un.d_ptr = 0;
12260 case DT_PREINIT_ARRAYSZ:
12261 name = ".preinit_array";
12263 case DT_INIT_ARRAYSZ:
12264 name = ".init_array";
12266 case DT_FINI_ARRAYSZ:
12267 name = ".fini_array";
12269 o = bfd_get_section_by_name (abfd, name);
12273 (_("could not find section %s"), name);
12278 (_("warning: %s section has zero size"), name);
12279 dyn.d_un.d_val = o->size;
12282 case DT_PREINIT_ARRAY:
12283 name = ".preinit_array";
12285 case DT_INIT_ARRAY:
12286 name = ".init_array";
12288 case DT_FINI_ARRAY:
12289 name = ".fini_array";
12291 o = bfd_get_section_by_name (abfd, name);
12298 name = ".gnu.hash";
12307 name = ".gnu.version_d";
12310 name = ".gnu.version_r";
12313 name = ".gnu.version";
12315 o = bfd_get_linker_section (dynobj, name);
12320 (_("could not find section %s"), name);
12323 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12326 (_("warning: section '%s' is being made into a note"), name);
12327 bfd_set_error (bfd_error_nonrepresentable_section);
12330 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12337 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12343 for (i = 1; i < elf_numsections (abfd); i++)
12345 Elf_Internal_Shdr *hdr;
12347 hdr = elf_elfsections (abfd)[i];
12348 if (hdr->sh_type == type
12349 && (hdr->sh_flags & SHF_ALLOC) != 0)
12351 sh_size += hdr->sh_size;
12353 || sh_addr > hdr->sh_addr)
12354 sh_addr = hdr->sh_addr;
12358 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12360 /* Don't count procedure linkage table relocs in the
12361 overall reloc count. */
12362 sh_size -= htab->srelplt->size;
12364 /* If the size is zero, make the address zero too.
12365 This is to avoid a glibc bug. If the backend
12366 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12367 zero, then we'll put DT_RELA at the end of
12368 DT_JMPREL. glibc will interpret the end of
12369 DT_RELA matching the end of DT_JMPREL as the
12370 case where DT_RELA includes DT_JMPREL, and for
12371 LD_BIND_NOW will decide that processing DT_RELA
12372 will process the PLT relocs too. Net result:
12373 No PLT relocs applied. */
12376 /* If .rela.plt is the first .rela section, exclude
12377 it from DT_RELA. */
12378 else if (sh_addr == (htab->srelplt->output_section->vma
12379 + htab->srelplt->output_offset))
12380 sh_addr += htab->srelplt->size;
12383 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12384 dyn.d_un.d_val = sh_size;
12386 dyn.d_un.d_ptr = sh_addr;
12389 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12393 /* If we have created any dynamic sections, then output them. */
12394 if (dynobj != NULL)
12396 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12399 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12400 if (((info->warn_shared_textrel && bfd_link_pic (info))
12401 || info->error_textrel)
12402 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12404 bfd_byte *dyncon, *dynconend;
12406 dyncon = o->contents;
12407 dynconend = o->contents + o->size;
12408 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12410 Elf_Internal_Dyn dyn;
12412 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12414 if (dyn.d_tag == DT_TEXTREL)
12416 if (info->error_textrel)
12417 info->callbacks->einfo
12418 (_("%P%X: read-only segment has dynamic relocations.\n"));
12420 info->callbacks->einfo
12421 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12427 for (o = dynobj->sections; o != NULL; o = o->next)
12429 if ((o->flags & SEC_HAS_CONTENTS) == 0
12431 || o->output_section == bfd_abs_section_ptr)
12433 if ((o->flags & SEC_LINKER_CREATED) == 0)
12435 /* At this point, we are only interested in sections
12436 created by _bfd_elf_link_create_dynamic_sections. */
12439 if (htab->stab_info.stabstr == o)
12441 if (htab->eh_info.hdr_sec == o)
12443 if (strcmp (o->name, ".dynstr") != 0)
12445 if (! bfd_set_section_contents (abfd, o->output_section,
12447 (file_ptr) o->output_offset
12448 * bfd_octets_per_byte (abfd),
12454 /* The contents of the .dynstr section are actually in a
12458 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12459 if (bfd_seek (abfd, off, SEEK_SET) != 0
12460 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12466 if (bfd_link_relocatable (info))
12468 bfd_boolean failed = FALSE;
12470 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12475 /* If we have optimized stabs strings, output them. */
12476 if (htab->stab_info.stabstr != NULL)
12478 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12482 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12485 elf_final_link_free (abfd, &flinfo);
12487 elf_linker (abfd) = TRUE;
12491 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12492 if (contents == NULL)
12493 return FALSE; /* Bail out and fail. */
12494 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12495 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12502 elf_final_link_free (abfd, &flinfo);
12506 /* Initialize COOKIE for input bfd ABFD. */
12509 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12510 struct bfd_link_info *info, bfd *abfd)
12512 Elf_Internal_Shdr *symtab_hdr;
12513 const struct elf_backend_data *bed;
12515 bed = get_elf_backend_data (abfd);
12516 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12518 cookie->abfd = abfd;
12519 cookie->sym_hashes = elf_sym_hashes (abfd);
12520 cookie->bad_symtab = elf_bad_symtab (abfd);
12521 if (cookie->bad_symtab)
12523 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12524 cookie->extsymoff = 0;
12528 cookie->locsymcount = symtab_hdr->sh_info;
12529 cookie->extsymoff = symtab_hdr->sh_info;
12532 if (bed->s->arch_size == 32)
12533 cookie->r_sym_shift = 8;
12535 cookie->r_sym_shift = 32;
12537 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12538 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12540 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12541 cookie->locsymcount, 0,
12543 if (cookie->locsyms == NULL)
12545 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12548 if (info->keep_memory)
12549 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12554 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12557 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12559 Elf_Internal_Shdr *symtab_hdr;
12561 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12562 if (cookie->locsyms != NULL
12563 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12564 free (cookie->locsyms);
12567 /* Initialize the relocation information in COOKIE for input section SEC
12568 of input bfd ABFD. */
12571 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12572 struct bfd_link_info *info, bfd *abfd,
12575 const struct elf_backend_data *bed;
12577 if (sec->reloc_count == 0)
12579 cookie->rels = NULL;
12580 cookie->relend = NULL;
12584 bed = get_elf_backend_data (abfd);
12586 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12587 info->keep_memory);
12588 if (cookie->rels == NULL)
12590 cookie->rel = cookie->rels;
12591 cookie->relend = (cookie->rels
12592 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12594 cookie->rel = cookie->rels;
12598 /* Free the memory allocated by init_reloc_cookie_rels,
12602 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12605 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12606 free (cookie->rels);
12609 /* Initialize the whole of COOKIE for input section SEC. */
12612 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12613 struct bfd_link_info *info,
12616 if (!init_reloc_cookie (cookie, info, sec->owner))
12618 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12623 fini_reloc_cookie (cookie, sec->owner);
12628 /* Free the memory allocated by init_reloc_cookie_for_section,
12632 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12635 fini_reloc_cookie_rels (cookie, sec);
12636 fini_reloc_cookie (cookie, sec->owner);
12639 /* Garbage collect unused sections. */
12641 /* Default gc_mark_hook. */
12644 _bfd_elf_gc_mark_hook (asection *sec,
12645 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12646 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12647 struct elf_link_hash_entry *h,
12648 Elf_Internal_Sym *sym)
12652 switch (h->root.type)
12654 case bfd_link_hash_defined:
12655 case bfd_link_hash_defweak:
12656 return h->root.u.def.section;
12658 case bfd_link_hash_common:
12659 return h->root.u.c.p->section;
12666 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12671 /* For undefined __start_<name> and __stop_<name> symbols, return the
12672 first input section matching <name>. Return NULL otherwise. */
12675 _bfd_elf_is_start_stop (const struct bfd_link_info *info,
12676 struct elf_link_hash_entry *h)
12679 const char *sec_name;
12681 if (h->root.type != bfd_link_hash_undefined
12682 && h->root.type != bfd_link_hash_undefweak)
12685 s = h->root.u.undef.section;
12688 if (s == (asection *) 0 - 1)
12694 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12695 sec_name = h->root.root.string + 8;
12696 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12697 sec_name = h->root.root.string + 7;
12699 if (sec_name != NULL && *sec_name != '\0')
12703 for (i = info->input_bfds; i != NULL; i = i->link.next)
12705 s = bfd_get_section_by_name (i, sec_name);
12708 h->root.u.undef.section = s;
12715 h->root.u.undef.section = (asection *) 0 - 1;
12720 /* COOKIE->rel describes a relocation against section SEC, which is
12721 a section we've decided to keep. Return the section that contains
12722 the relocation symbol, or NULL if no section contains it. */
12725 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12726 elf_gc_mark_hook_fn gc_mark_hook,
12727 struct elf_reloc_cookie *cookie,
12728 bfd_boolean *start_stop)
12730 unsigned long r_symndx;
12731 struct elf_link_hash_entry *h;
12733 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12734 if (r_symndx == STN_UNDEF)
12737 if (r_symndx >= cookie->locsymcount
12738 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12740 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12743 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12747 while (h->root.type == bfd_link_hash_indirect
12748 || h->root.type == bfd_link_hash_warning)
12749 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12751 /* If this symbol is weak and there is a non-weak definition, we
12752 keep the non-weak definition because many backends put
12753 dynamic reloc info on the non-weak definition for code
12754 handling copy relocs. */
12755 if (h->u.weakdef != NULL)
12756 h->u.weakdef->mark = 1;
12758 if (start_stop != NULL)
12760 /* To work around a glibc bug, mark all XXX input sections
12761 when there is an as yet undefined reference to __start_XXX
12762 or __stop_XXX symbols. The linker will later define such
12763 symbols for orphan input sections that have a name
12764 representable as a C identifier. */
12765 asection *s = _bfd_elf_is_start_stop (info, h);
12769 *start_stop = !s->gc_mark;
12774 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12777 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12778 &cookie->locsyms[r_symndx]);
12781 /* COOKIE->rel describes a relocation against section SEC, which is
12782 a section we've decided to keep. Mark the section that contains
12783 the relocation symbol. */
12786 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12788 elf_gc_mark_hook_fn gc_mark_hook,
12789 struct elf_reloc_cookie *cookie)
12792 bfd_boolean start_stop = FALSE;
12794 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12795 while (rsec != NULL)
12797 if (!rsec->gc_mark)
12799 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12800 || (rsec->owner->flags & DYNAMIC) != 0)
12802 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12807 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12812 /* The mark phase of garbage collection. For a given section, mark
12813 it and any sections in this section's group, and all the sections
12814 which define symbols to which it refers. */
12817 _bfd_elf_gc_mark (struct bfd_link_info *info,
12819 elf_gc_mark_hook_fn gc_mark_hook)
12822 asection *group_sec, *eh_frame;
12826 /* Mark all the sections in the group. */
12827 group_sec = elf_section_data (sec)->next_in_group;
12828 if (group_sec && !group_sec->gc_mark)
12829 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12832 /* Look through the section relocs. */
12834 eh_frame = elf_eh_frame_section (sec->owner);
12835 if ((sec->flags & SEC_RELOC) != 0
12836 && sec->reloc_count > 0
12837 && sec != eh_frame)
12839 struct elf_reloc_cookie cookie;
12841 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12845 for (; cookie.rel < cookie.relend; cookie.rel++)
12846 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12851 fini_reloc_cookie_for_section (&cookie, sec);
12855 if (ret && eh_frame && elf_fde_list (sec))
12857 struct elf_reloc_cookie cookie;
12859 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12863 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12864 gc_mark_hook, &cookie))
12866 fini_reloc_cookie_for_section (&cookie, eh_frame);
12870 eh_frame = elf_section_eh_frame_entry (sec);
12871 if (ret && eh_frame && !eh_frame->gc_mark)
12872 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12878 /* Scan and mark sections in a special or debug section group. */
12881 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12883 /* Point to first section of section group. */
12885 /* Used to iterate the section group. */
12888 bfd_boolean is_special_grp = TRUE;
12889 bfd_boolean is_debug_grp = TRUE;
12891 /* First scan to see if group contains any section other than debug
12892 and special section. */
12893 ssec = msec = elf_next_in_group (grp);
12896 if ((msec->flags & SEC_DEBUGGING) == 0)
12897 is_debug_grp = FALSE;
12899 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12900 is_special_grp = FALSE;
12902 msec = elf_next_in_group (msec);
12904 while (msec != ssec);
12906 /* If this is a pure debug section group or pure special section group,
12907 keep all sections in this group. */
12908 if (is_debug_grp || is_special_grp)
12913 msec = elf_next_in_group (msec);
12915 while (msec != ssec);
12919 /* Keep debug and special sections. */
12922 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12923 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12927 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12930 bfd_boolean some_kept;
12931 bfd_boolean debug_frag_seen;
12933 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12936 /* Ensure all linker created sections are kept,
12937 see if any other section is already marked,
12938 and note if we have any fragmented debug sections. */
12939 debug_frag_seen = some_kept = FALSE;
12940 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12942 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12944 else if (isec->gc_mark)
12947 if (debug_frag_seen == FALSE
12948 && (isec->flags & SEC_DEBUGGING)
12949 && CONST_STRNEQ (isec->name, ".debug_line."))
12950 debug_frag_seen = TRUE;
12953 /* If no section in this file will be kept, then we can
12954 toss out the debug and special sections. */
12958 /* Keep debug and special sections like .comment when they are
12959 not part of a group. Also keep section groups that contain
12960 just debug sections or special sections. */
12961 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12963 if ((isec->flags & SEC_GROUP) != 0)
12964 _bfd_elf_gc_mark_debug_special_section_group (isec);
12965 else if (((isec->flags & SEC_DEBUGGING) != 0
12966 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12967 && elf_next_in_group (isec) == NULL)
12971 if (! debug_frag_seen)
12974 /* Look for CODE sections which are going to be discarded,
12975 and find and discard any fragmented debug sections which
12976 are associated with that code section. */
12977 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12978 if ((isec->flags & SEC_CODE) != 0
12979 && isec->gc_mark == 0)
12984 ilen = strlen (isec->name);
12986 /* Association is determined by the name of the debug section
12987 containing the name of the code section as a suffix. For
12988 example .debug_line.text.foo is a debug section associated
12990 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12994 if (dsec->gc_mark == 0
12995 || (dsec->flags & SEC_DEBUGGING) == 0)
12998 dlen = strlen (dsec->name);
13001 && strncmp (dsec->name + (dlen - ilen),
13002 isec->name, ilen) == 0)
13012 /* The sweep phase of garbage collection. Remove all garbage sections. */
13014 typedef bfd_boolean (*gc_sweep_hook_fn)
13015 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
13018 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13021 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13022 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
13024 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13028 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13029 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13032 for (o = sub->sections; o != NULL; o = o->next)
13034 /* When any section in a section group is kept, we keep all
13035 sections in the section group. If the first member of
13036 the section group is excluded, we will also exclude the
13038 if (o->flags & SEC_GROUP)
13040 asection *first = elf_next_in_group (o);
13041 o->gc_mark = first->gc_mark;
13047 /* Skip sweeping sections already excluded. */
13048 if (o->flags & SEC_EXCLUDE)
13051 /* Since this is early in the link process, it is simple
13052 to remove a section from the output. */
13053 o->flags |= SEC_EXCLUDE;
13055 if (info->print_gc_sections && o->size != 0)
13056 /* xgettext:c-format */
13057 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13060 /* But we also have to update some of the relocation
13061 info we collected before. */
13063 && (o->flags & SEC_RELOC) != 0
13064 && o->reloc_count != 0
13065 && !((info->strip == strip_all || info->strip == strip_debugger)
13066 && (o->flags & SEC_DEBUGGING) != 0)
13067 && !bfd_is_abs_section (o->output_section))
13069 Elf_Internal_Rela *internal_relocs;
13073 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
13074 info->keep_memory);
13075 if (internal_relocs == NULL)
13078 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
13080 if (elf_section_data (o)->relocs != internal_relocs)
13081 free (internal_relocs);
13092 /* Propagate collected vtable information. This is called through
13093 elf_link_hash_traverse. */
13096 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13098 /* Those that are not vtables. */
13099 if (h->vtable == NULL || h->vtable->parent == NULL)
13102 /* Those vtables that do not have parents, we cannot merge. */
13103 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
13106 /* If we've already been done, exit. */
13107 if (h->vtable->used && h->vtable->used[-1])
13110 /* Make sure the parent's table is up to date. */
13111 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
13113 if (h->vtable->used == NULL)
13115 /* None of this table's entries were referenced. Re-use the
13117 h->vtable->used = h->vtable->parent->vtable->used;
13118 h->vtable->size = h->vtable->parent->vtable->size;
13123 bfd_boolean *cu, *pu;
13125 /* Or the parent's entries into ours. */
13126 cu = h->vtable->used;
13128 pu = h->vtable->parent->vtable->used;
13131 const struct elf_backend_data *bed;
13132 unsigned int log_file_align;
13134 bed = get_elf_backend_data (h->root.u.def.section->owner);
13135 log_file_align = bed->s->log_file_align;
13136 n = h->vtable->parent->vtable->size >> log_file_align;
13151 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13154 bfd_vma hstart, hend;
13155 Elf_Internal_Rela *relstart, *relend, *rel;
13156 const struct elf_backend_data *bed;
13157 unsigned int log_file_align;
13159 /* Take care of both those symbols that do not describe vtables as
13160 well as those that are not loaded. */
13161 if (h->vtable == NULL || h->vtable->parent == NULL)
13164 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13165 || h->root.type == bfd_link_hash_defweak);
13167 sec = h->root.u.def.section;
13168 hstart = h->root.u.def.value;
13169 hend = hstart + h->size;
13171 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13173 return *(bfd_boolean *) okp = FALSE;
13174 bed = get_elf_backend_data (sec->owner);
13175 log_file_align = bed->s->log_file_align;
13177 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13179 for (rel = relstart; rel < relend; ++rel)
13180 if (rel->r_offset >= hstart && rel->r_offset < hend)
13182 /* If the entry is in use, do nothing. */
13183 if (h->vtable->used
13184 && (rel->r_offset - hstart) < h->vtable->size)
13186 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13187 if (h->vtable->used[entry])
13190 /* Otherwise, kill it. */
13191 rel->r_offset = rel->r_info = rel->r_addend = 0;
13197 /* Mark sections containing dynamically referenced symbols. When
13198 building shared libraries, we must assume that any visible symbol is
13202 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13204 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13205 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13207 if ((h->root.type == bfd_link_hash_defined
13208 || h->root.type == bfd_link_hash_defweak)
13210 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13211 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13212 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13213 && (!bfd_link_executable (info)
13214 || info->gc_keep_exported
13215 || info->export_dynamic
13218 && (*d->match) (&d->head, NULL, h->root.root.string)))
13219 && (h->versioned >= versioned
13220 || !bfd_hide_sym_by_version (info->version_info,
13221 h->root.root.string)))))
13222 h->root.u.def.section->flags |= SEC_KEEP;
13227 /* Keep all sections containing symbols undefined on the command-line,
13228 and the section containing the entry symbol. */
13231 _bfd_elf_gc_keep (struct bfd_link_info *info)
13233 struct bfd_sym_chain *sym;
13235 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13237 struct elf_link_hash_entry *h;
13239 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13240 FALSE, FALSE, FALSE);
13243 && (h->root.type == bfd_link_hash_defined
13244 || h->root.type == bfd_link_hash_defweak)
13245 && !bfd_is_abs_section (h->root.u.def.section)
13246 && !bfd_is_und_section (h->root.u.def.section))
13247 h->root.u.def.section->flags |= SEC_KEEP;
13252 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13253 struct bfd_link_info *info)
13255 bfd *ibfd = info->input_bfds;
13257 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13260 struct elf_reloc_cookie cookie;
13262 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13265 if (!init_reloc_cookie (&cookie, info, ibfd))
13268 for (sec = ibfd->sections; sec; sec = sec->next)
13270 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13271 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13273 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13274 fini_reloc_cookie_rels (&cookie, sec);
13281 /* Do mark and sweep of unused sections. */
13284 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13286 bfd_boolean ok = TRUE;
13288 elf_gc_mark_hook_fn gc_mark_hook;
13289 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13290 struct elf_link_hash_table *htab;
13292 if (!bed->can_gc_sections
13293 || !is_elf_hash_table (info->hash))
13295 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13299 bed->gc_keep (info);
13300 htab = elf_hash_table (info);
13302 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13303 at the .eh_frame section if we can mark the FDEs individually. */
13304 for (sub = info->input_bfds;
13305 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13306 sub = sub->link.next)
13309 struct elf_reloc_cookie cookie;
13311 sec = bfd_get_section_by_name (sub, ".eh_frame");
13312 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13314 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13315 if (elf_section_data (sec)->sec_info
13316 && (sec->flags & SEC_LINKER_CREATED) == 0)
13317 elf_eh_frame_section (sub) = sec;
13318 fini_reloc_cookie_for_section (&cookie, sec);
13319 sec = bfd_get_next_section_by_name (NULL, sec);
13323 /* Apply transitive closure to the vtable entry usage info. */
13324 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13328 /* Kill the vtable relocations that were not used. */
13329 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13333 /* Mark dynamically referenced symbols. */
13334 if (htab->dynamic_sections_created || info->gc_keep_exported)
13335 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13337 /* Grovel through relocs to find out who stays ... */
13338 gc_mark_hook = bed->gc_mark_hook;
13339 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13343 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13344 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13347 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13348 Also treat note sections as a root, if the section is not part
13350 for (o = sub->sections; o != NULL; o = o->next)
13352 && (o->flags & SEC_EXCLUDE) == 0
13353 && ((o->flags & SEC_KEEP) != 0
13354 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13355 && elf_next_in_group (o) == NULL )))
13357 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13362 /* Allow the backend to mark additional target specific sections. */
13363 bed->gc_mark_extra_sections (info, gc_mark_hook);
13365 /* ... and mark SEC_EXCLUDE for those that go. */
13366 return elf_gc_sweep (abfd, info);
13369 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13372 bfd_elf_gc_record_vtinherit (bfd *abfd,
13374 struct elf_link_hash_entry *h,
13377 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13378 struct elf_link_hash_entry **search, *child;
13379 size_t extsymcount;
13380 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13382 /* The sh_info field of the symtab header tells us where the
13383 external symbols start. We don't care about the local symbols at
13385 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13386 if (!elf_bad_symtab (abfd))
13387 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13389 sym_hashes = elf_sym_hashes (abfd);
13390 sym_hashes_end = sym_hashes + extsymcount;
13392 /* Hunt down the child symbol, which is in this section at the same
13393 offset as the relocation. */
13394 for (search = sym_hashes; search != sym_hashes_end; ++search)
13396 if ((child = *search) != NULL
13397 && (child->root.type == bfd_link_hash_defined
13398 || child->root.type == bfd_link_hash_defweak)
13399 && child->root.u.def.section == sec
13400 && child->root.u.def.value == offset)
13404 /* xgettext:c-format */
13405 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13406 abfd, sec, (unsigned long) offset);
13407 bfd_set_error (bfd_error_invalid_operation);
13411 if (!child->vtable)
13413 child->vtable = ((struct elf_link_virtual_table_entry *)
13414 bfd_zalloc (abfd, sizeof (*child->vtable)));
13415 if (!child->vtable)
13420 /* This *should* only be the absolute section. It could potentially
13421 be that someone has defined a non-global vtable though, which
13422 would be bad. It isn't worth paging in the local symbols to be
13423 sure though; that case should simply be handled by the assembler. */
13425 child->vtable->parent = (struct elf_link_hash_entry *) -1;
13428 child->vtable->parent = h;
13433 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13436 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13437 asection *sec ATTRIBUTE_UNUSED,
13438 struct elf_link_hash_entry *h,
13441 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13442 unsigned int log_file_align = bed->s->log_file_align;
13446 h->vtable = ((struct elf_link_virtual_table_entry *)
13447 bfd_zalloc (abfd, sizeof (*h->vtable)));
13452 if (addend >= h->vtable->size)
13454 size_t size, bytes, file_align;
13455 bfd_boolean *ptr = h->vtable->used;
13457 /* While the symbol is undefined, we have to be prepared to handle
13459 file_align = 1 << log_file_align;
13460 if (h->root.type == bfd_link_hash_undefined)
13461 size = addend + file_align;
13465 if (addend >= size)
13467 /* Oops! We've got a reference past the defined end of
13468 the table. This is probably a bug -- shall we warn? */
13469 size = addend + file_align;
13472 size = (size + file_align - 1) & -file_align;
13474 /* Allocate one extra entry for use as a "done" flag for the
13475 consolidation pass. */
13476 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13480 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13486 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13487 * sizeof (bfd_boolean));
13488 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13492 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13497 /* And arrange for that done flag to be at index -1. */
13498 h->vtable->used = ptr + 1;
13499 h->vtable->size = size;
13502 h->vtable->used[addend >> log_file_align] = TRUE;
13507 /* Map an ELF section header flag to its corresponding string. */
13511 flagword flag_value;
13512 } elf_flags_to_name_table;
13514 static elf_flags_to_name_table elf_flags_to_names [] =
13516 { "SHF_WRITE", SHF_WRITE },
13517 { "SHF_ALLOC", SHF_ALLOC },
13518 { "SHF_EXECINSTR", SHF_EXECINSTR },
13519 { "SHF_MERGE", SHF_MERGE },
13520 { "SHF_STRINGS", SHF_STRINGS },
13521 { "SHF_INFO_LINK", SHF_INFO_LINK},
13522 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13523 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13524 { "SHF_GROUP", SHF_GROUP },
13525 { "SHF_TLS", SHF_TLS },
13526 { "SHF_MASKOS", SHF_MASKOS },
13527 { "SHF_EXCLUDE", SHF_EXCLUDE },
13530 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13532 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13533 struct flag_info *flaginfo,
13536 const bfd_vma sh_flags = elf_section_flags (section);
13538 if (!flaginfo->flags_initialized)
13540 bfd *obfd = info->output_bfd;
13541 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13542 struct flag_info_list *tf = flaginfo->flag_list;
13544 int without_hex = 0;
13546 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13549 flagword (*lookup) (char *);
13551 lookup = bed->elf_backend_lookup_section_flags_hook;
13552 if (lookup != NULL)
13554 flagword hexval = (*lookup) ((char *) tf->name);
13558 if (tf->with == with_flags)
13559 with_hex |= hexval;
13560 else if (tf->with == without_flags)
13561 without_hex |= hexval;
13566 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13568 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13570 if (tf->with == with_flags)
13571 with_hex |= elf_flags_to_names[i].flag_value;
13572 else if (tf->with == without_flags)
13573 without_hex |= elf_flags_to_names[i].flag_value;
13580 info->callbacks->einfo
13581 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13585 flaginfo->flags_initialized = TRUE;
13586 flaginfo->only_with_flags |= with_hex;
13587 flaginfo->not_with_flags |= without_hex;
13590 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13593 if ((flaginfo->not_with_flags & sh_flags) != 0)
13599 struct alloc_got_off_arg {
13601 struct bfd_link_info *info;
13604 /* We need a special top-level link routine to convert got reference counts
13605 to real got offsets. */
13608 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13610 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13611 bfd *obfd = gofarg->info->output_bfd;
13612 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13614 if (h->got.refcount > 0)
13616 h->got.offset = gofarg->gotoff;
13617 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13620 h->got.offset = (bfd_vma) -1;
13625 /* And an accompanying bit to work out final got entry offsets once
13626 we're done. Should be called from final_link. */
13629 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13630 struct bfd_link_info *info)
13633 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13635 struct alloc_got_off_arg gofarg;
13637 BFD_ASSERT (abfd == info->output_bfd);
13639 if (! is_elf_hash_table (info->hash))
13642 /* The GOT offset is relative to the .got section, but the GOT header is
13643 put into the .got.plt section, if the backend uses it. */
13644 if (bed->want_got_plt)
13647 gotoff = bed->got_header_size;
13649 /* Do the local .got entries first. */
13650 for (i = info->input_bfds; i; i = i->link.next)
13652 bfd_signed_vma *local_got;
13653 size_t j, locsymcount;
13654 Elf_Internal_Shdr *symtab_hdr;
13656 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13659 local_got = elf_local_got_refcounts (i);
13663 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13664 if (elf_bad_symtab (i))
13665 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13667 locsymcount = symtab_hdr->sh_info;
13669 for (j = 0; j < locsymcount; ++j)
13671 if (local_got[j] > 0)
13673 local_got[j] = gotoff;
13674 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13677 local_got[j] = (bfd_vma) -1;
13681 /* Then the global .got entries. .plt refcounts are handled by
13682 adjust_dynamic_symbol */
13683 gofarg.gotoff = gotoff;
13684 gofarg.info = info;
13685 elf_link_hash_traverse (elf_hash_table (info),
13686 elf_gc_allocate_got_offsets,
13691 /* Many folk need no more in the way of final link than this, once
13692 got entry reference counting is enabled. */
13695 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13697 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13700 /* Invoke the regular ELF backend linker to do all the work. */
13701 return bfd_elf_final_link (abfd, info);
13705 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13707 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13709 if (rcookie->bad_symtab)
13710 rcookie->rel = rcookie->rels;
13712 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13714 unsigned long r_symndx;
13716 if (! rcookie->bad_symtab)
13717 if (rcookie->rel->r_offset > offset)
13719 if (rcookie->rel->r_offset != offset)
13722 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13723 if (r_symndx == STN_UNDEF)
13726 if (r_symndx >= rcookie->locsymcount
13727 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13729 struct elf_link_hash_entry *h;
13731 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13733 while (h->root.type == bfd_link_hash_indirect
13734 || h->root.type == bfd_link_hash_warning)
13735 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13737 if ((h->root.type == bfd_link_hash_defined
13738 || h->root.type == bfd_link_hash_defweak)
13739 && (h->root.u.def.section->owner != rcookie->abfd
13740 || h->root.u.def.section->kept_section != NULL
13741 || discarded_section (h->root.u.def.section)))
13746 /* It's not a relocation against a global symbol,
13747 but it could be a relocation against a local
13748 symbol for a discarded section. */
13750 Elf_Internal_Sym *isym;
13752 /* Need to: get the symbol; get the section. */
13753 isym = &rcookie->locsyms[r_symndx];
13754 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13756 && (isec->kept_section != NULL
13757 || discarded_section (isec)))
13765 /* Discard unneeded references to discarded sections.
13766 Returns -1 on error, 1 if any section's size was changed, 0 if
13767 nothing changed. This function assumes that the relocations are in
13768 sorted order, which is true for all known assemblers. */
13771 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13773 struct elf_reloc_cookie cookie;
13778 if (info->traditional_format
13779 || !is_elf_hash_table (info->hash))
13782 o = bfd_get_section_by_name (output_bfd, ".stab");
13787 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13790 || i->reloc_count == 0
13791 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13795 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13798 if (!init_reloc_cookie_for_section (&cookie, info, i))
13801 if (_bfd_discard_section_stabs (abfd, i,
13802 elf_section_data (i)->sec_info,
13803 bfd_elf_reloc_symbol_deleted_p,
13807 fini_reloc_cookie_for_section (&cookie, i);
13812 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13813 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13817 int eh_changed = 0;
13819 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13825 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13828 if (!init_reloc_cookie_for_section (&cookie, info, i))
13831 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13832 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13833 bfd_elf_reloc_symbol_deleted_p,
13837 if (i->size != i->rawsize)
13841 fini_reloc_cookie_for_section (&cookie, i);
13844 elf_link_hash_traverse (elf_hash_table (info),
13845 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
13848 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13850 const struct elf_backend_data *bed;
13852 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13855 bed = get_elf_backend_data (abfd);
13857 if (bed->elf_backend_discard_info != NULL)
13859 if (!init_reloc_cookie (&cookie, info, abfd))
13862 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13865 fini_reloc_cookie (&cookie, abfd);
13869 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13870 _bfd_elf_end_eh_frame_parsing (info);
13872 if (info->eh_frame_hdr_type
13873 && !bfd_link_relocatable (info)
13874 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13881 _bfd_elf_section_already_linked (bfd *abfd,
13883 struct bfd_link_info *info)
13886 const char *name, *key;
13887 struct bfd_section_already_linked *l;
13888 struct bfd_section_already_linked_hash_entry *already_linked_list;
13890 if (sec->output_section == bfd_abs_section_ptr)
13893 flags = sec->flags;
13895 /* Return if it isn't a linkonce section. A comdat group section
13896 also has SEC_LINK_ONCE set. */
13897 if ((flags & SEC_LINK_ONCE) == 0)
13900 /* Don't put group member sections on our list of already linked
13901 sections. They are handled as a group via their group section. */
13902 if (elf_sec_group (sec) != NULL)
13905 /* For a SHT_GROUP section, use the group signature as the key. */
13907 if ((flags & SEC_GROUP) != 0
13908 && elf_next_in_group (sec) != NULL
13909 && elf_group_name (elf_next_in_group (sec)) != NULL)
13910 key = elf_group_name (elf_next_in_group (sec));
13913 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13914 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13915 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13918 /* Must be a user linkonce section that doesn't follow gcc's
13919 naming convention. In this case we won't be matching
13920 single member groups. */
13924 already_linked_list = bfd_section_already_linked_table_lookup (key);
13926 for (l = already_linked_list->entry; l != NULL; l = l->next)
13928 /* We may have 2 different types of sections on the list: group
13929 sections with a signature of <key> (<key> is some string),
13930 and linkonce sections named .gnu.linkonce.<type>.<key>.
13931 Match like sections. LTO plugin sections are an exception.
13932 They are always named .gnu.linkonce.t.<key> and match either
13933 type of section. */
13934 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13935 && ((flags & SEC_GROUP) != 0
13936 || strcmp (name, l->sec->name) == 0))
13937 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13939 /* The section has already been linked. See if we should
13940 issue a warning. */
13941 if (!_bfd_handle_already_linked (sec, l, info))
13944 if (flags & SEC_GROUP)
13946 asection *first = elf_next_in_group (sec);
13947 asection *s = first;
13951 s->output_section = bfd_abs_section_ptr;
13952 /* Record which group discards it. */
13953 s->kept_section = l->sec;
13954 s = elf_next_in_group (s);
13955 /* These lists are circular. */
13965 /* A single member comdat group section may be discarded by a
13966 linkonce section and vice versa. */
13967 if ((flags & SEC_GROUP) != 0)
13969 asection *first = elf_next_in_group (sec);
13971 if (first != NULL && elf_next_in_group (first) == first)
13972 /* Check this single member group against linkonce sections. */
13973 for (l = already_linked_list->entry; l != NULL; l = l->next)
13974 if ((l->sec->flags & SEC_GROUP) == 0
13975 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13977 first->output_section = bfd_abs_section_ptr;
13978 first->kept_section = l->sec;
13979 sec->output_section = bfd_abs_section_ptr;
13984 /* Check this linkonce section against single member groups. */
13985 for (l = already_linked_list->entry; l != NULL; l = l->next)
13986 if (l->sec->flags & SEC_GROUP)
13988 asection *first = elf_next_in_group (l->sec);
13991 && elf_next_in_group (first) == first
13992 && bfd_elf_match_symbols_in_sections (first, sec, info))
13994 sec->output_section = bfd_abs_section_ptr;
13995 sec->kept_section = first;
14000 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14001 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14002 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14003 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14004 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14005 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14006 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14007 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14008 The reverse order cannot happen as there is never a bfd with only the
14009 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14010 matter as here were are looking only for cross-bfd sections. */
14012 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14013 for (l = already_linked_list->entry; l != NULL; l = l->next)
14014 if ((l->sec->flags & SEC_GROUP) == 0
14015 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14017 if (abfd != l->sec->owner)
14018 sec->output_section = bfd_abs_section_ptr;
14022 /* This is the first section with this name. Record it. */
14023 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14024 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14025 return sec->output_section == bfd_abs_section_ptr;
14029 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14031 return sym->st_shndx == SHN_COMMON;
14035 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14041 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14043 return bfd_com_section_ptr;
14047 _bfd_elf_default_got_elt_size (bfd *abfd,
14048 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14049 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14050 bfd *ibfd ATTRIBUTE_UNUSED,
14051 unsigned long symndx ATTRIBUTE_UNUSED)
14053 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14054 return bed->s->arch_size / 8;
14057 /* Routines to support the creation of dynamic relocs. */
14059 /* Returns the name of the dynamic reloc section associated with SEC. */
14061 static const char *
14062 get_dynamic_reloc_section_name (bfd * abfd,
14064 bfd_boolean is_rela)
14067 const char *old_name = bfd_get_section_name (NULL, sec);
14068 const char *prefix = is_rela ? ".rela" : ".rel";
14070 if (old_name == NULL)
14073 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14074 sprintf (name, "%s%s", prefix, old_name);
14079 /* Returns the dynamic reloc section associated with SEC.
14080 If necessary compute the name of the dynamic reloc section based
14081 on SEC's name (looked up in ABFD's string table) and the setting
14085 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14087 bfd_boolean is_rela)
14089 asection * reloc_sec = elf_section_data (sec)->sreloc;
14091 if (reloc_sec == NULL)
14093 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14097 reloc_sec = bfd_get_linker_section (abfd, name);
14099 if (reloc_sec != NULL)
14100 elf_section_data (sec)->sreloc = reloc_sec;
14107 /* Returns the dynamic reloc section associated with SEC. If the
14108 section does not exist it is created and attached to the DYNOBJ
14109 bfd and stored in the SRELOC field of SEC's elf_section_data
14112 ALIGNMENT is the alignment for the newly created section and
14113 IS_RELA defines whether the name should be .rela.<SEC's name>
14114 or .rel.<SEC's name>. The section name is looked up in the
14115 string table associated with ABFD. */
14118 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14120 unsigned int alignment,
14122 bfd_boolean is_rela)
14124 asection * reloc_sec = elf_section_data (sec)->sreloc;
14126 if (reloc_sec == NULL)
14128 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14133 reloc_sec = bfd_get_linker_section (dynobj, name);
14135 if (reloc_sec == NULL)
14137 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14138 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14139 if ((sec->flags & SEC_ALLOC) != 0)
14140 flags |= SEC_ALLOC | SEC_LOAD;
14142 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14143 if (reloc_sec != NULL)
14145 /* _bfd_elf_get_sec_type_attr chooses a section type by
14146 name. Override as it may be wrong, eg. for a user
14147 section named "auto" we'll get ".relauto" which is
14148 seen to be a .rela section. */
14149 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14150 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14155 elf_section_data (sec)->sreloc = reloc_sec;
14161 /* Copy the ELF symbol type and other attributes for a linker script
14162 assignment from HSRC to HDEST. Generally this should be treated as
14163 if we found a strong non-dynamic definition for HDEST (except that
14164 ld ignores multiple definition errors). */
14166 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14167 struct bfd_link_hash_entry *hdest,
14168 struct bfd_link_hash_entry *hsrc)
14170 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14171 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14172 Elf_Internal_Sym isym;
14174 ehdest->type = ehsrc->type;
14175 ehdest->target_internal = ehsrc->target_internal;
14177 isym.st_other = ehsrc->other;
14178 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14181 /* Append a RELA relocation REL to section S in BFD. */
14184 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14186 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14187 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14188 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14189 bed->s->swap_reloca_out (abfd, rel, loc);
14192 /* Append a REL relocation REL to section S in BFD. */
14195 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14197 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14198 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14199 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14200 bed->s->swap_reloc_out (abfd, rel, loc);